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\n",ans);
    }
    return 0;
}

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

转载于:https://www.cnblogs.com/2000liuwei/p/10486785.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值