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 }
  • 相关阅读:
    Android组件化框架设计与实践
    浅谈Android进阶之路
    Android APP 性能优化的一些思考
    有关 Hybrid 开发模式实践总结
    开发人员必备的技能——单元测试
    有关Android插件化思考
    人生的意义到底是什么?
    Asp.Net Core 3.1学习-读取、监听json配置文件(7)
    Asp.Net Core 3.1学习-依赖注入、服务生命周期(6)
    Asp.Net Core 3.1学习- 应用程序的启动过程(5)
  • 原文地址:https://www.cnblogs.com/jishuren/p/12239587.html
Copyright © 2011-2022 走看看