首页 > 其他分享 >Draw! (思维)

Draw! (思维)

时间:2023-02-17 12:55:25浏览次数:40  
标签:思维 Draw moments LL number score 平局 match


                                                                                  Draw!

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

Each of the next nn lines contains integers aiai and bibi (0≤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

Copy


3 2 0 3 1 3 4


Output

Copy


2


Input

Copy


3 0 0 0 0 0 0


Output

Copy


1


Input

Copy


1 5 4


Output

Copy


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.

一道思维题 ,在足球比赛中, 任意n个时刻给你 场上的比分, 求最大平局的次数 . 

 考虑什么时候会出现平局呢?比如  场上 比分 x : y (假设 x >y  , 比如  2:1 )  , 下一次是 (3:4 ) , 那么中间是存在平局现象的. 

 2:1 ->  2:2 ->3:2 -> 3:3 .....  出现两个 . 就是说 如果下一次是平局 设为(m:m), 那么 m 一定大于等于上一局的最大值 , 如果下一次不是平局设为(x1:y1) 那么这两次观察出现平局的最大次数为 min(x1,y1) -max(x,y) 这中间包含了 (m:m) ,所以在判断时候,

如果出现上一次观察室平局, 那么就要减去1去掉重复的 ; 

#include<iostream>
#include <cstdio>
using namespace std ;
typedef long long LL ;
int main(){
int n ;
LL ans = 1 ;
cin >> n ;
LL a,b ;
a = b = 0 ;
for(int i = 1 ; i<=n ; i++ ){
LL x , y ;
cin >>x >>y ;
LL m1 = min(x,y) ; // 当前比分最小的
LL m2 = max(a,b) ; // 上一次比分最大的
if(m1>=m2){
ans+=(m1-m2+1) ;
}
if(a == b){
ans-- ;
}
a = x ;
b = y ;
}
// 想想什么时候出现平局 ;
// 假设 当前 x : y ; (x>y )
// 那么下一次出现平均为 m :m ;(m>=y )
// x1 : y1
cout<<ans ;
return 0 ;
}

 

标签:思维,Draw,moments,LL,number,score,平局,match
From: https://blog.51cto.com/u_15970235/6063951

相关文章

  • 计算思维的门槛:递归思想
    今天做题发现了一道对递归最佳辅助理解的题目:剑指Offer68-II.二叉树的最近公共祖先,二叉树对理解递归的帮助很大。1/**2*Definitionforabinarytreenod......
  • 6张思维导图理清电视剧狂飙剧情
    你今天‘飙’了吗?” “我的‘飙瘾’又犯了,求求快点更新。” “不是在走亲访友的路上,就是在追《狂飙》电视剧的途中。” 这是狂飙更新时,大家的状态。 《狂飙......
  • 计算机网络思维导图
    前言lxl做的思维导图,浅偷一下思维导图......
  • 拓展思维的一题多解题
    前言在新人教A版看到这个经典的题目[1],联想到以前的相应解法,现对各种思路作以总结,体会下:我们积累的数学知识越多,解题的思路就越开阔,越顺畅。典例剖析如图所示,已知平......
  • 2023-02-14 量学基础 就近主力思维 分时筹码分布,在附近爆量的地方特别好用 89
    分时筹码分布,先来看一个最近的案例。这里重要的是放大量。1.DDL在昨天冲高回落,放出了百日高量。虽然它的平均建仓成本就在5-7块钱,但是并不知道为何突然爆量(q4业绩刚出)2.......
  • 翻硬币 【 字符串 | 思维 】
    翻硬币Description小明正在玩一个“翻硬币”的游戏。桌上放着排成一排的若干硬币。我们用*表示正面,用o表示反面(是小写字母,不是零)。比如,可能情形是:**oo***oooo如果同时翻......
  • Java面向对象部分思维导图
         ......
  • leangoo领歌共享协作思维导图导入导出XMind文件。
    协作型共享多人协作思维导图Leangoo,可以导出导入XMind文件了,我们来看看如何操作:进入leangoo官网:www.leangoo.com,登陆账号。点击右上角“+”新建脑图,然后导入XMind文件......
  • 硬件思维---信号在脑子中应该是怎么样的?
     我们先来思考下这个问题:你认为正弦波和方波哪个波形最简单? 以前的话,认为方波更简单,现在的话,认为正弦波更简单。方波就是高低电平成比例变化,自然以前会认为简单。但......
  • 使用draw.io绘制流程图
    前言:draw.io是一款成熟的通用型图表工具,免登录、免费、无广告、并且开源安装网页端:链接客户端:阿里云盘教程b站链接图文精简版Draw.io使用技巧图文精简版:链接......