欢迎访问https://blog.csdn.net/lxt_Lucia~~
宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~
一、问题:
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 Input1
3
2 0
3 1
3 4
Sample Output1
2
Sample Input2
3
0 0
0 0
0 0
Sample Output2
1
Sample Input3
1
5 4
Sample Output3
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.
二、题意:
按照时间顺序给出比赛期间的几组比分,x:y,求比赛期间可能出现的最多平局数。
三、思路:
由于输入是按照时间顺序给出的,因此仅需遍历一遍,每次只需计算当前比分与上一次的比分之间可能出现的平局数,并求和。而每两次比分间能出现平局的条件为:当前比分中的较小值>=等于上一次比分的较大值,且注意,如果上一次给出的比分即为平局,则重复计算了一次,再减1即可。
四、代码:
#include <cstdio>
#include <iostream>
#define read(x) scanf("%d",&x)
#define fori(a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long LL;
struct A
{
LL x,y;
};
int main()
{
int n;
read(n);
struct A a[n];
fori(0,n-1)
scanf("%lld %lld",&a[i].x,&a[i].y);
LL sum=min(a[0].x,a[0].y)+1;
fori(1,n-1)
{
if(min(a[i].x,a[i].y)>=max(a[i-1].x,a[i-1].y))
sum+=min(a[i].x,a[i].y)-max(a[i-1].x,a[i-1].y)+1;
if(a[i-1].x==a[i-1].y)
sum--;
}
printf("%lld\n",sum);
return 0;
}