CodeForces 1131B(思维题)

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 (1n100001≤n≤10000) — the number of known moments in the match.

Each of the next nn lines contains integers aiai and bibi (0ai,bi1090≤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
3
2 0
3 1
3 4
Output
2
Input
3
0 0
0 0
0 0
Output
1
Input
1
5 4
Output
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.

说实话,我不会做这道题,看了别人的后才会写的。。。

题意:两个球队比分,给出几个时间的比分,要你求最多的比分是平的次数。题目简单易懂,然后就是思考了,纯粹的思维题。。

思路:对于第一组数据,判断更小的一个,然后更小的肯定是比平局次数少一了,因为(0,0)肯定是一组。然后就是找出以后的每组数据的大的那一个,去比较前面的一组若是相同则也分情况去减前面的。

话不多说,看代码吧。、

#include<stdio.h>
#include<string.h>
int min(int x,int y)
{
    return x<y?x:y;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int x,y,a=0,b=0,ans=1;//a,b赋初值为0,以后用来表示每组数据前的一组x和y 
        for(int i=1;i<=n;i++)//因为存在(0,0),所以ans的初值为1 
        {
            scanf("%d%d",&x,&y);
            if(x==a&&y==b)
            continue;//如果x和y都与前面的a,b相等,说明x,y没变 
            if(a>b)//前面的x比现在的x大 
            {
            if(x>y){if(y>=a) ans+=y-a+1;}//还有一个y==a的情况,所以要加1 
            else ans+=x-a+1;//y>x的情况 
            }
            else if(a<b){
            if(x<y){if(x>=b) ans+=x-b+1;}
            else ans+=y-b+1;
        }
        else if(a==b){
            ans+=min(x,y)-a;
        }
        a=x,b=y;//更新每一次的a,b的值 
            
        }
        printf("%d
",ans);
    }
    return 0;
}

风咋起,合当奋意向人生!

原文地址:https://www.cnblogs.com/2000liuwei/p/10486785.html