Codeforces Round #541(Div.2) B. Draw!解题报告

题目链接

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

Each of the next n lines contains integers ai and bi (0 ≤ \leq ai,bi ≤ \leq 1 0 9 10^9 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 xi and yj 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.

题目大意
两支球队A,B进行足球比赛,按顺寻给你不同时刻的足球比分,对于某一时刻分数发生改变,要么A球队加一分,要么B球队加一分。问:在比赛的过程中两支球队分数相等的最多次数。

解题思路
说实话,这是一道水题,水题,水题。这么道水题,自己在比赛的时候居然没有想到。假设在某一时刻比分为x:y(x,y的大小关系未知)。在此,为了方便理解,我们假设x>y,那么要实现平局的情况的话,平局的分数w应该要 ≥ \geq x。假设在相邻的下一时刻比分为m:n,那么平局的分数w不应该超过min(m,n) 即(w ≤ \leq min(n,m))。那么两个时刻之间平局的可能就是(min(n,m)-max(x,y)),但是这儿有一个前提就是min(n,m) ≥ \geq max(x,y)

AC代码

#include <bits/stdc++.h>
#include<stdlib.h>
#define INF 0x3f3f3f
using namespace std;
const int mod=1e9+7;
const int Max_N=10000+5;
typedef pair<int,int>P;
typedef long long ll;
typedef unsigned long long ull;
int main(int argc, char const *argv[])
{
    int n;
    cin>>n;
    int lx=0,ly=0;
    ll ans=1;
    while(n--)
    {
        int x,y;
        cin>>x>>y;
        int mid1=max(lx,ly);
        int mid2=min(x,y);
        if(mid1<=mid2)//必须要判断一下相对大小
            ans+=mid2-mid1+1;
        if(lx==ly)ans--;//如果lx==ly的话,在上一次就已经计算过lx==ly的情况。
        //在这次中又重复计算了一次,所以要执行ans--这个操作;
        lx=x,ly=y;
    }
    cout<<ans<<endl;
    return 0;
}

总结
菜是原罪。思维题的话要理解到问题的本,并且要考虑到特殊的情况。虽然这道题的样例中给了个特殊数据,但自己在思考的过程中也要去考虑,尽量把所有情况都能考虑到。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值