Codeforces 1131 B. Draw!-暴力

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 (1n100001≤n≤10000) — the number of known moments in the match.

Each of the next nn lines contains integers aiai and bibi (0ai,bi1090≤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.

题意就是按时间顺序给你比赛的比分,让你算有几次平局。

代码:

 1 //B
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 typedef long long ll;
 5 const int maxn=1e5+10;
 6 
 7 int main()
 8 {
 9     ll n;
10     cin>>n;
11     ll ans=1,a=0,b=0;
12     for(int i=0;i<n;i++){
13         ll x,y;
14         cin>>x>>y;
15         if(x==a&&y==b) continue;
16         if(a>b){
17             if(x>y){if(y>=a) ans+=y-a+1;}
18             else ans+=x-a+1;
19         }
20         else if(a<b){
21             if(x<y){if(x>=b) ans+=x-b+1;}
22             else ans+=y-b+1;
23         }
24         else if(a==b){
25             ans+=min(x,y)-a;
26         }
27         a=x,b=y;
28     }
29     cout<<ans<<endl;
30 }

猜你喜欢

转载自www.cnblogs.com/ZERO-/p/10426426.html