Draw!
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 nn (1≤n≤10000 ) — the number of known moments in the match.
Each of the next nn lines contains integers aiai and bibi (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 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.
Examples
Input
Copy
3 2 0 3 1 3 4
Output
Copy
2
Input
Copy
3 0 0 0 0 0 0
Output
Copy
1
Input
Copy
1 5 4
Output
Copy
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.
一道思维题 ,在足球比赛中, 任意n个时刻给你 场上的比分, 求最大平局的次数 .
考虑什么时候会出现平局呢?比如 场上 比分 x : y (假设 x >y , 比如 2:1 ) , 下一次是 (3:4 ) , 那么中间是存在平局现象的.
2:1 -> 2:2 ->3:2 -> 3:3 ..... 出现两个 . 就是说 如果下一次是平局 设为(m:m), 那么 m 一定大于等于上一局的最大值 , 如果下一次不是平局设为(x1:y1) 那么这两次观察出现平局的最大次数为 min(x1,y1) -max(x,y) 这中间包含了 (m:m) ,所以在判断时候,
如果出现上一次观察室平局, 那么就要减去1去掉重复的 ;
#include<iostream>
#include <cstdio>
using namespace std ;
typedef long long LL ;
int main(){
int n ;
LL ans = 1 ;
cin >> n ;
LL a,b ;
a = b = 0 ;
for(int i = 1 ; i<=n ; i++ ){
LL x , y ;
cin >>x >>y ;
LL m1 = min(x,y) ; // 当前比分最小的
LL m2 = max(a,b) ; // 上一次比分最大的
if(m1>=m2){
ans+=(m1-m2+1) ;
}
if(a == b){
ans-- ;
}
a = x ;
b = y ;
}
// 想想什么时候出现平局 ;
// 假设 当前 x : y ; (x>y )
// 那么下一次出现平均为 m :m ;(m>=y )
// x1 : y1
cout<<ans ;
return 0 ;
}
标签:思维,Draw,moments,LL,number,score,平局,match From: https://blog.51cto.com/u_15970235/6063951