CF 1131B Draw!

                         Draw!
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
 

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 (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.

Sample Input

Input
3
2 0
3 1
3 4
Output
2
Input
3
0 0
0 0
0 0
Output
1
Input
1
5 4
Output
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\n",ans);
92     return 0;
93 }
View Code

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章