zoukankan      html  css  js  c++  java
  • B

    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.

     1 #include <iostream>
     2 #include <cstdio>
     3 
     4 using namespace std;
     5 
     6 const int max_N = 1e4+10;
     7 
     8 int a,b;
     9 int n;
    10 
    11 int main()
    12 {
    13     scanf("%d",&n);
    14     int min_num=0,max_num=0,ans=1,before_a=0,before_b=0;
    15     
    16     // 注意(x,x)的特殊情况判断
    17 
    18     for(int i=0;i<n;++i)
    19     {
    20         scanf("%d %d",&a,&b);
    21         min_num=min(a,b);
    22         if(min_num>=max_num)
    23         {
    24             ans+=1 + min_num - max_num;
    25         }
    26 
    27         if(before_a==before_b)
    28         {
    29             --ans;
    30         }
    31 
    32         before_a=a,before_b=b;
    33 
    34         max_num=max(a,b);
    35     }
    36     printf("%d",ans);
    37     return 0;
    38 }
  • 相关阅读:
    WIN32弹窗报时间【CSDN】
    函数返回类型为引用的好处
    计算机补码的秘密【转】
    设计模式之Singleton模式【类只有一个对象】
    bitwise constness与logical constness【EC++笔记】
    避免使用隐式类型转换【转】
    private, public, protected,继承【转】
    nonlocal static对象初始化【EC++笔记】
    在资源管理类中提供对原始资源的访问【EC++笔记】
    copy构造函数与copy赋值操作符【EC++笔记】
  • 原文地址:https://www.cnblogs.com/jishuren/p/12239587.html
Copyright © 2011-2022 走看看