B. Draw!
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You still have partial information about the score during the historic football match. You are given a set of pairs (ai,bi), indicating that at some point during the match the score was “ai: bi”. It is known that if the current score is «x:y», then after the goal it will change to “x+1:y” or “x:y+1”. What is the largest number of times a draw could appear on the scoreboard?
The pairs “ai:bi” 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 n (1≤n≤10000) — the number of known moments in the match.
Each of the next n lines contains integers ai and bi (0≤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 xi and yj 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
inputCopy
3
2 0
3 1
3 4
outputCopy
2
inputCopy
3
0 0
0 0
0 0
outputCopy
1
inputCopy
1
5 4
outputCopy
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.
题目翻译:两个人进行比赛,输入是按照时间来看的比赛比分情况,求最大可能平局的情况
题目思路:前面一眼的比分情况最大值a,和第二眼比分情况最小值b,中间必然可以存在a:a,a+1:a+1,……b:b
重点是讨论好一些特殊的情况,比如输入是第一眼1:0,第二眼2:2,第三眼3:3等。
#include<iostream>
using namespace std;
int main()
{
long long a = 0,b = 0;
int n;
cin >> n;
long long ans = 1;
while(n--)
{
long long aa,bb;
cin >> aa >> bb;
if(aa == a && bb == b)continue;
ans += max(0LL,min(bb,aa) - max(a,b)+1);
if(a == b) ans--;
a = aa;
b = bb;
}
return 0;
}