【思维】draw!

版权声明:版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/YT201758501112/article/details/88283045

题目:

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.

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,再接着输入n组数代表比赛过程中的比分,求在存在要求的比分下最多能有多少场平局,注:0:0也算一场平局。

解决方法:由第一次给出的比分可确定在此之前的平局总数最大为ans=min(x,y)+1,此后从第二次开始到结束,设当前比分为x:y,前一次比分为lx:ly,由此可判断,若x<ly||y<ly,则证明这两次之间无平局出现,若lx=ly,则平局数为ans+=min(x-lx,y-ly),若lx>ly,则ans+=min(x,y)-lx+1,若lx<ly,则ans+=min(x,y)-ly+1。

AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int main() 
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    int n;
    cin>>n;
    int ans=1;
    int x,y,lx,ly;
    cin>>x>>y;
    ans+=min(x,y);
    lx=x;ly=y;
    rep(i,2,n) {
    	cin>>x>>y;
    	if(x<ly||y<lx) {lx=x;ly=y;continue;}
    	if(lx==ly) ans+=min(x-lx,y-ly);
    	else if(lx>ly) {
    		if(x>=y) ans+=y-lx+1;
    		else ans+=x-lx+1;
    	}
    	else {
    		if(y>=x) ans+=x-ly+1;
    		else ans+=y-ly+1;
    	}
     	lx=x;ly=y;
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/YT201758501112/article/details/88283045
今日推荐