Description
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.
Sample Input
3
2 0
3 1
3 4
2
3
0 0
0 0
0 0
1
1
5 4
5
Hint
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.
题意:
两个人进行比赛,给出几场两人的比分,问在满足这几场比分的条件下,最多会出现几场平局(开始的0:0也算一场)。
解题思路:
将比赛双方看成两条不断前进的线段,利用区间的方式去模拟比赛的情况。
重点是Debug,在思路找不到错误的情况下,就去多试一些样例,尽量的做到测试的不重不漏。
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 #include<queue> 8 #include<stack> 9 #include<deque> 10 #include<map> 11 #include<iostream> 12 using namespace std; 13 typedef long long LL; 14 const double pi=acos(-1.0); 15 const double e=exp(1); 16 const int N = 100009; 17 18 19 queue<LL> check; 20 int main() 21 { 22 LL i,p,j,n,t; 23 LL ans = 0; 24 LL a1,b1,a2,b2; 25 26 while(!check.empty()) 27 check.pop(); 28 29 scanf("%lld",&n); 30 scanf("%lld%lld",&a1,&b1); 31 32 ans += min(a1, b1) + 1; 33 check.push(min(a1,b1)); 34 for(i = 1; i < n; i ++) 35 { 36 scanf("%lld%lld",&a2,&b2); 37 if(a2 < b1 || a1 > b2) 38 ; 39 else if(a1 < b1 && a2 <= b2 && a2 >= b1) 40 { 41 ans+=(a2 - b1) + 1; 42 43 44 if(check.front() == b1) 45 { 46 ans--; 47 48 } 49 check.pop(); 50 check.push(a2); 51 } 52 else if(a1 >= b1 && a2 <= b2) 53 { 54 ans+=(a2 - a1 + 1); 55 56 if(check.front() == a1) 57 { 58 ans--; 59 60 } 61 check.pop(); 62 check.push(a2); 63 } 64 else if(a1 >= b1 && a1 <= b2 && a2 > b2) 65 { 66 ans += b2 - a1 + 1; 67 68 if(check.front() == a1) 69 { 70 ans--; 71 72 } 73 check.pop(); 74 check.push(b2); 75 } 76 else if(a1 <= b1 && a2 >= b2) 77 { 78 ans += b2 - b1 + 1; 79 80 if(check.front() == b1) 81 { 82 ans--; 83 84 } 85 check.pop(); 86 check.push(b2); 87 } 88 a1 = a2; 89 b1 = b2; 90 } 91 printf("%lld ",ans); 92 return 0; 93 }