CodeForces 1131B Draw!

F - Draw!

https://codeforces.com/problemset/problem/1131/B

Description

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

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
3
0 0
0 0
0 0
1
5 4

Sample Output

2
1
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.

题意

水题一道,输入在某个时刻的两队球队的得分,问相同的分数的时刻至多会有多少个。

#include
#include
using namespace std;
int main()
{
	int n,x,y;
	int sum1=0,sum2=0,ans=1;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d",&x,&y);
		if(sum1==x&&sum2==y) continue;
		else {
			if(sum1==sum2) {
				ans+=(min(x,y)-sum1);
			}
			else {//假设min(x,y)是x,x一定大于sum1,
				int sum=(min(x,y)-max(sum1,sum2)+1);//但x不一定大于sum2,
				if(sum>0) ans+=sum;//因此判断一下sum的值

			}
			sum1=x;
			sum2=y;
		}
	}
	printf("%d\n",ans);
	return 0;
}

你可能感兴趣的:(CodeForces 1131B Draw!)