A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet knows the schedule: when each of the dishes will be served.

For i-th of the dishes he knows two integer moments in time ai and bi (in seconds from the beginning of the banquet) — when the cooks will bring the i-th dish into the hall and when they will carry it out (ai < bi). For example, if ai = 10 and bi = 11, then the i-th dish is available for eating during one second.

The dishes come in very large quantities, so it is guaranteed that as long as the dish is available for eating (i. e. while it is in the hall) it cannot run out.

The gourmet wants to try each of the n dishes and not to offend any of the cooks. Because of that the gourmet wants to eat each of the dishes for the same amount of time. During eating the gourmet can instantly switch between the dishes. Switching between dishes is allowed for him only at integer moments in time. The gourmet can eat no more than one dish simultaneously. It is allowed to return to a dish after eating any other dishes.

The gourmet wants to eat as long as possible on the banquet without violating any conditions described above. Can you help him and find out the maximum total time he can eat the dishes on the banquet?

Input

The first line of input contains an integer n (1 ≤ n ≤ 100) — the number of dishes on the banquet.

The following n lines contain information about availability of the dishes. The i-th line contains two integers ai and bi (0 ≤ ai < bi ≤ 10000) — the moments in time when the i-th dish becomes available for eating and when the i-th dish is taken away from the hall.

Output

Output should contain the only integer — the maximum total time the gourmet can eat the dishes on the banquet.

The gourmet can instantly switch between the dishes but only at integer moments in time. It is allowed to return to a dish after eating any other dishes. Also in every moment in time he can eat no more than one dish.

Examples

Input
3
2 4
1 5
6 9
Output
6
Input
3
1 2
1 2
1 2
Output
0

Note

In the first example the gourmet eats the second dish for one second (from the moment in time 1 to the moment in time 2), then he eats the first dish for two seconds (from 2 to 4), then he returns to the second dish for one second (from 4 to 5). After that he eats the third dish for two seconds (from 6 to 8).

In the second example the gourmet cannot eat each dish for at least one second because there are three dishes but they are available for only one second (from 1 to 2).

题意:给出了n个时间段,你需要在每个时间段中挑选相同长度的一段(不需要连续),问你挑选的最长时间段的总和是多少。比如第一个样例,第一段挑选2-4两秒,第二段挑选1-2,4-5两秒,第三段挑选6-8两秒,一共6秒,而第二个样例连一秒都分不出,所以0。

思路:用二分枚举0到题目限制的最大值,判断每次二分的中点是否可以,找出满足条件的最大值。

判断是否可以的方法是对每个时间段的终点进行从小到大排序,然后标记它需要占用的时间,已被占用的就跳过。为什么对终点进行排序,因为这样就能够做到不浪费每一秒,对时间进行合理的安排。若对起点进行排序,将有可能使得后面本可以安排的时间都无法安排,例如第一个样例。

代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#define eps 1e-7
#define ll long long
#define inf 0x3f3f3f3f
#define pi 3.141592653589793238462643383279
using namespace std;
const int MAXN = ;
struct node{
int beg,end;
}num[]; int cmp(node a,node b)
{
if(a.end != b.end)
return a.end < b.end;
else return a.beg < b.beg;
} bool judge(int n,int len)
{
int visit[MAXN];
memset(visit,,sizeof(visit));
for(int i=; i<n; ++i) //遍历每一个时间段
{
int cnt = ;
for(int j=num[i].beg; j<num[i].end; ++j) //安排时间,标记占用
{
if(!visit[j])
{
visit[j] = ;
cnt++;
}
if(cnt == len) break;
}
if(cnt < len) return false;
}
return true;
} int main()
{
int n;
while(cin>>n)
{
for(int i=; i<n; ++i)
scanf("%d%d",&num[i].beg,&num[i].end);
sort(num,num+n,cmp);//排序
int low = ,high = MAXN;
while(low <= high) //二分
{
int mid = (low + high)/;
if(judge(n,mid))
low = mid + ;
else high = mid - ;
}
cout<<(high*n)<<endl; //n个时间段,每个都占用了相同大小的一段
}
return ;
}

CodeForces - 589F —(二分+贪心)的更多相关文章

  1. Codeforces 732D [二分 ][贪心]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...

  2. CodeForces - 551C 二分+贪心

    题意:有n个箱子形成的堆,现在有m个学生,每个学生每一秒可以有两种操作: 1: 向右移动一格 2: 移除当前位置的一个箱子 求移除所有箱子需要的最短时间.注意:所有学生可以同时行动. 思路:二分时间, ...

  3. Codeforces 825D 二分贪心

    题意:给一个 s 串和 t 串, s 串中有若干问号,问如何填充问号使得 s 串中字母可以组成最多的 t 串.输出填充后的 s 串. 思路:想了下感觉直接怼有点麻烦,要分情况:先处理已经可以组成 t ...

  4. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  5. 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心

    /** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...

  6. CodeForces - 158B.Taxi (贪心)

    CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...

  7. Codeforces 589F Gourmet and Banquet

    A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...

  8. 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心

    题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径. ...

  9. Codeforces_732D_(二分贪心)

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  10. CF732D Exams 二分 贪心

    思路:二分+贪心 提交次数:10次以上 错因:刚开始以为二分(边界,$+1or-1$)写错了,调了半天,后来才发现是$ck()$写错了.开始只判了最后是否小于零,而应该中间一旦小于零就$return\ ...

随机推荐

  1. 【linux】du命令

    Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项][文件] 2.命令功能 ...

  2. 善待Erlang 代码 -- Xref 实践

    Xref 是一个交叉引用工具,通过分析定义的函数间的调用关系,用于查找函数. 模块. 应用程序和版本之间的依赖关系. 通俗而言,Xref 可以检查代码中函数的调用关系.比如在 moduleA 中的 f ...

  3. vue-router和锚点冲突问题

    传统的锚点定位会与vue-router中的路由设置存在冲突,都是使用'#'进行的,所以这里使用一直方法来模拟锚点跳转,并使用tween.js达到动态的过度效果 不使用原生锚点,使用这种方式解决 imp ...

  4. nohup 后台运行命令

    在Linux上部署zipkin,在SSH客户端执行java -jar zipkin-server-1.21.0-exec.jar,启动成功,在关闭SSH客户端后,运行的程序也同时终止了,怎样才能保证在 ...

  5. CFGym 101490E 题解

    一.题目链接 http://codeforces.com/gym/101490 二.题面 三.题意 给你一个图,n个点,m条边,一个x,从顶点1走到顶点n.假设从顶点1走到顶点n的最短路为d,x代表你 ...

  6. http和https(转)

    一.HTTP协议 最近看了一些网络通信方面的书籍,研究了一下 HTTP 和 TCP/IP,有了一些新的收获和理解,在这里做个归纳和总结. (1)什么是HTTP协议 HTTP (HyperText Tr ...

  7. 28.OGNL与ValueStack(VS)-总结$ # %的区别

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html $用于i18n和struts配置文件 #取得ActionContext的值 ...

  8. 一劳永逸解决VLC播放中文字幕乱码问题

    VLC对于Mac/Ubuntu用户来说算得上是必备软件.其相当于PC机上的“暴风影音”,但Mac/Ubuntu的新手使用VLC播放avi时都会碰 到字幕乱码的问题.avi字幕的格式有多种,这里假设你使 ...

  9. C# 之 日常问题积累

    https://www.cnblogs.com/xinaixia/p/3956349.html

  10. PatBlt

    PatBlt     该函数使用当前选入指定设备环境中的刷子绘制给定的矩形区域.通过使用给出的光栅操作来对该刷子的像素和表面像素进行组合. 原型: BOOL PatBlt(     HDC hdc,  ...