Draw!

A. Draw!

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

 

题意: 最多多少次打成平局

想了好久,才有 区间相交的思路  交了好几次都是细节问题  很难受啊

#include<bits/stdc++.h>
using namespace std;
long long  a[10005];
long long  b[10005];
int main()
{
    int n;
    cin>>n;
    a[0]=0;
    b[0]=0;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        cin>>b[i];
    }
    long long  sum=1;
    for(int i=0;i<n;i++)
    {
        long long  maxx=max(a[i],b[i]);
        long long minn=min(a[i+1],b[i+1]);
        if(minn>=maxx)
            {sum+=minn-maxx+1;
                if(a[i]==b[i])sum-=1;
            }
    }
    cout<<sum<<endl;




    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值