日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【思维】draw!

發布時間:2025/3/8 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【思维】draw! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目:

You still have partial information about the score during the historic football match. You are given a set of pairs?(ai,bi)(ai,bi), indicating that at some point during the match the score was "aiai:?bibi". It is known that if the current score is ?xx:yy?, then after the goal it will change to "x+1x+1:yy" or "xx:y+1y+1". What is the largest number of times a draw could appear on the scoreboard?

The pairs "aiai:bibi" are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match.

Input

The first line contains a single integer?nn?(1≤n≤100001≤n≤10000) — the number of known moments in the match.

Each of the next?nn?lines contains integers?aiai?and?bibi?(0≤ai,bi≤1090≤ai,bi≤109), denoting the score of the match at that moment (that is, the number of goals by the first team and the number of goals by the second team).

All moments are given in chronological order, that is, sequences?xixi?and?yjyj?are non-decreasing. The last score denotes the final result of the match.

Output

Print the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted.

Examples

input

Copy

3 2 0 3 1 3 4

output

Copy

2

input

Copy

3 0 0 0 0 0 0

output

Copy

1

input

Copy

1 5 4

output

Copy

5

Note

In the example one of the possible score sequences leading to the maximum number of draws is as follows: 0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4.

題目大意:先輸入一個整數n,再接著輸入n組數代表比賽過程中的比分,求在存在要求的比分下最多能有多少場平局,注:0:0也算一場平局。

解決方法:由第一次給出的比分可確定在此之前的平局總數最大為ans=min(x,y)+1,此后從第二次開始到結束,設當前比分為x:y,前一次比分為lx:ly,由此可判斷,若x<ly||y<ly,則證明這兩次之間無平局出現,若lx=ly,則平局數為ans+=min(x-lx,y-ly),若lx>ly,則ans+=min(x,y)-lx+1,若lx<ly,則ans+=min(x,y)-ly+1。

AC代碼:

#include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <map> #include <stack> #include <queue> #include <vector> #include <bitset> #include <set> #include <utility> #include <sstream> #include <iomanip> using namespace std; typedef long long ll; typedef unsigned long long ull; #define inf 0x3f3f3f3f #define rep(i,l,r) for(int i=l;i<=r;i++) #define lep(i,l,r) for(int i=l;i>=r;i--) #define ms(arr) memset(arr,0,sizeof(arr)) //priority_queue<int,vector<int> ,greater<int> >q; const int maxn = (int)1e5 + 5; const ll mod = 1e9+7; int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int n;cin>>n;int ans=1;int x,y,lx,ly;cin>>x>>y;ans+=min(x,y);lx=x;ly=y;rep(i,2,n) {cin>>x>>y;if(x<ly||y<lx) {lx=x;ly=y;continue;}if(lx==ly) ans+=min(x-lx,y-ly);else if(lx>ly) {if(x>=y) ans+=y-lx+1;else ans+=x-lx+1;}else {if(y>=x) ans+=x-ly+1;else ans+=y-ly+1;}lx=x;ly=y;}cout<<ans<<endl;return 0; }

?

總結

以上是生活随笔為你收集整理的【思维】draw!的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。