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. CentOS6.5 安装codeblocks-13.12

    安装环境CentOS6.5 启动命令行 1.先安装gcc和gcc++,这个可以直接安装 # yum install gcc # yum install gcc-c++ 2.再安装gtk2,也是直接安装 ...

  2. 面试总结之C/C++

    source code https://github.com/haotang923/interview/blob/master/interview%20summary%20of%20C%20and%2 ...

  3. CentOS下长时间ping网络加时间戳并记录到文本

    Linux下长时间ping网络加时间戳并记录到文本   由于一些原因,比如需要检查网络之间是否存在掉包等问题,会长时间去ping一个地址,由于会输出大量的信息而且最好要有时间戳,因此我们可以使用简单的 ...

  4. RTNETLINK answers: File exists

    问题: 重启网络时报错如下 >>/etc/init.d/network start RTNETLINK answers: File exists 分析: /etc/init.d/netwo ...

  5. sqlserver基础操作

    启动服务: 1.在系统服务启动 2.在sql配置管理器服务选项中启动 3.在管理员cmd:net start mssqlserver;net stop mssqlserver use master g ...

  6. Python中小整数对象池和大整数对象池

    1. 小整数对象池 整数在程序中的使用非常广泛,Python为了优化速度,使用了小整数对象池, 避免为整数频繁申请和销毁内存空间. Python 对小整数的定义是 [-5, 256] 这些整数对象是提 ...

  7. iOS开发系列-ARC浅解

    一.什么是 ARC ? 所谓ARC就是Automatic Reference Counting , 即自动引用计数.ARC是自iOS5引入的.ARC机制的引入是为了简化开发过程的内存管理的.相对于之前 ...

  8. ubantu环境下fidder安装

    转自:http://www.cnblogs.com/jcli/p/4474332.html Linux(Ubuntu)环境下使用Fiddler 自己的开发环境是Ubuntu, 对于很多优秀的软件但是又 ...

  9. could not be installed at this time

    无法下载应用 此时无法安装 Unable to Download App ''App" could not be installed at this time 编译程序的时候,Target ...

  10. PowerDesigner 生成的脚本取掉双引号

    建模工具PowerDesigner http://www.cnblogs.com/mcgrady/archive/2013/05/25/3098588.html 默认导出在带双引号,表名称后期使用的时 ...