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 (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

32 03 13 4

Output

2

Input

30 00 00 0

Output

1

Input

15 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

时间: 2024-11-03 22:21:21

CodeForces 1131B(思维题)的相关文章

CodeForces - 417B (思维题)

Crash Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description During the "Russian Code Cup" programming competition, the testing system stores all sent solutions for each participant. We know th

CodeForces - 417A(思维题)

Elimination Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description The finalists of the "Russian Code Cup" competition in 2214 will be the participants who win in one of the elimination rounds.

Codeforces 1270C 思维题

C. Make Good time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Let's call an array a1,a2,…,ama1,a2,…,am of nonnegative integer numbers good if a1+a2+?+am=2⋅(a1⊕a2⊕?⊕am)a1+a2+?+am=2⋅(a1⊕a2⊕?⊕

贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

题目传送门 1 /* 2 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 3 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0),先从1开始找到已经套好的娃娃层数, 4 其他是2次操作,还要减去k-1个娃娃是只要套上就可以 5 详细解释:http://blog.csdn.net/firstlucker/article/details/46671251 6 */ 7 #include <cstdio> 8 #i

codeforces 848B Rooter&#39;s Song 思维题

http://codeforces.com/problemset/problem/848/B 给定一个二维坐标系,点从横轴或纵轴垂直于发射的坐标轴射入(0,0)-(w,h)的矩形空间.给出点发射的坐标轴,位置,延迟时间,发生碰撞则交换方向.求最后每个点的射出位置. 首先我们观察能得出两个结论,1. 类似蚂蚁爬树枝的问题,相遇只会交换方向,所以最后的射出点集只会因为碰撞而改变动点与射出点的对应关系,而不会增加减少射出点集.2.我们根据其射入位置和延迟时间可以计算出一个值v=pos-time,只有这

Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) A. Contest for Robots(思维题)

Polycarp is preparing the first programming contest for robots. There are nn problems in it, and a lot of robots are going to participate in it. Each robot solving the problem ii gets pipi points, and the score of each robot in the competition is cal

Unique Encryption Keys (思维题 预处理)

题目 题意:给m个数字, q次询问, 询问b到e之间如果有重复数字就输出, 没有就输出OK 思路:用f[i]数组 记录从i开始向后最近的有重复数字的 位置, 如 1 3 2 2, 则f[1] = 4; 如果离a最近的重复数字的位置 都大于b, 就说明没有重复数字. f[]数组需要预处理,从后向前. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector>

sdut 2847 Monitor (思维题)

题目 题意:给定a, b, x, y;  求使c, d; 使c:d = x :y; 且c<=a, d<=b, 而且c, d尽量大. 先求最小倍数, 再用最小倍数乘 x, y; 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 7 long long gcd(long long a, l

hdu 4972 A simple dynamic programming problem (转化 乱搞 思维题) 2014多校10

题目链接 题意:给定一个数组记录两队之间分差,只记分差,不记谁高谁低,问最终有多少种比分的可能性 分析: 类似cf的题目,比赛的时候都没想出来,简直笨到极点..... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <vector> 7 #include &