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 }
  • 相关阅读:
    SpringMVC统一异常处理
    How to convert BigDecimal to Double in spring-data-mongodb framework
    DHCP动态主机配置协议
    你所听到的技术原理、技术本质到底是什么?
    前端技术及开发模式的演进,带你了解前端技术的前世今生
    金三银四,如何征服面试官,拿到Offer
    何谓多租户模式 ?
    骄傲的技术人,技术是你的全部吗?
    自我剖析,坚持有多难?
    从官方文档去学习之FreeMarker
  • 原文地址:https://www.cnblogs.com/jishuren/p/12239587.html
Copyright © 2011-2022 走看看