题目链接:http://codeforces.com/problemset/problem/589/F

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,代表有n种菜,下面n行代表每种菜上来的时间和下去的时间,要求你每道菜吃的时间一样多,问你最多可以吃多少多久,每一秒只能吃一道菜

思路:并不是自己的思路,自己不知道怎么贪心,大概猜出来是二分求值。。。 贪心的思想是按照每一道菜的端下去的时间从小到大排序,然后从前往后选择就行。。。这是为何呢?

因为我们要做的就是让选择的这道菜对其他菜影响最可能的少,然后我们按照端下去的时间排序,证明我们当前吃的菜端下去的时间是在其他菜端下去之前的,那么我们从前往后找时间是不是对其他菜影响最小呢?  当然是,接下来就看代码了

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e2+;
const int maxk=1e4+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1]
bool vis[maxk];
int n;
struct dish
{
int be,en;
}d[maxn];
bool cmp(const dish a,const dish b)
{
return a.en<b.en;
}
bool solve(int mid)
{
for(int i=;i<n;i++)
{
int sum=;
for(int j=d[i].be;j<d[i].en;j++)
{
if(!vis[j])
{
vis[j]=true;
sum++;
}
if(sum>=mid) break;
}
if(sum<mid)
return false;
}
return true;
}
int main()
{
memset(vis,false,sizeof(vis));
int mi=maxk;
cin>>n;
for(int i=;i<n;i++)
{
cin>>d[i].be>>d[i].en;
mi=min(abs(d[i].en-d[i].be),mi);
}
sort(d,d+n,cmp);
int l=,r=mi,mid=mi,ans=mid;
while(l<=r)
{
memset(vis,false,sizeof(vis));
if(solve(mid))
{
l=mid+;
ans=mid;
}
else
r=mid-;
mid=(l+r)/;
}
cout<<ans*n<<endl;
return ;
}

F. Gourmet and Banquet(贪心加二分求值)的更多相关文章

  1. Luogu2869 [USACO07DEC]美食的食草动物Gourmet Grazers (贪心,二分,数据结构优化)

    贪心 考场上因无优化与卡常T掉的\(n \log(n)\) //#include <iostream> #include <cstdio> #include <cstri ...

  2. 【CodeForces 589F】Gourmet and Banquet(二分+贪心或网络流)

    F. Gourmet and Banquet time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  3. BZOJ3527 推出卷积公式FFT求值

    BZOJ3527 推出卷积公式FFT求值 传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3527 题意: \(F_{j}=\sum_{i&l ...

  4. hdu 3641 数论 二分求符合条件的最小值数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...

  5. 九度OJ 1085 求root(N, k) -- 二分求幂及快速幂取模

    题目地址:http://ac.jobdu.com/problem.php?pid=1085 题目描述: N<k时,root(N,k) = N,否则,root(N,k) = root(N',k). ...

  6. hdu5256 二分求LIS+思维

    解题的思路很巧,为了让每个数之间都留出对应的上升空间,使a[i]=a[i]-i,然后再求LIS 另外二分求LIS是比较快的 #include<bits/stdc++.h> #define ...

  7. 二分求幂/快速幂取模运算——root(N,k)

    二分求幂 int getMi(int a,int b) { ; ) { //当二进制位k位为1时,需要累乘a的2^k次方,然后用ans保存 == ) { ans *= a; } a *= a; b / ...

  8. 二分求幂,快速求解a的b次幂

    一个引子 如何求得a的b次幂呢,那还不简单,一个for循环就可以实现! void main(void) { int a, b; ; cin >> a >> b; ; i < ...

  9. nyoj 409——郁闷的C小加(三)——————【中缀式化前缀后缀并求值】

    郁闷的C小加(三) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考“郁闷的C小加(一)”),C小加很 ...

随机推荐

  1. 洛谷【P1561】[USACO12JAN]爬山Mountain Climbing

    我对\(Jhonson\)算法的理解:https://www.cnblogs.com/AKMer/p/9863620.html 题目传送门:https://www.luogu.org/problemn ...

  2. bzoj 3144 切糕 —— 最小割

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3144 每个点拆成 R 个,连成一条链,边上是权值,割掉代表选这一层: 然后每个点的第 t 层 ...

  3. EXPLAIN 命令

    MySQL EXPLAIN 命令详解 MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提 ...

  4. 我的SIP开发之路

    http://hi.baidu.com/ltlovelty/blog/item/837baf1ece7fc6f11ad57647.html 经过对SIP协议和开源协议栈快半年的研究,我现在终于有点入门 ...

  5. UDK编辑器 49条小提示

    转自:http://www.cnblogs.com/hmxp8/archive/2012/02/09/2343674.html Very Helpful~ 01. First time using t ...

  6. JAVA 编程思想三

    1:JAVA可变参数? 参数个数不确定,但是类型确定: 可变参数位于最后一项,只支持一个可变参数: public void funciton1(int a, String ...args) { for ...

  7. Ruby 局部变量做block参数

    Ruby中使用yield语句调用block时可以带有参数,参数值见传送个相关联的block.如果传给block的参数是已经存在的局部变量,那么这些变量即为block的参数,他们的值可能会因block的 ...

  8. 具体问题:Spring 事务的隔离性,并说说每个隔离性的区别

    使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...

  9. 又见GCD (HDU 2504)

    这个题真的很水,但我竟然连错,在此警醒自己!!! 写代码改了东边,忘了西边,“认真”这两个字又被我吃了,打脸啪啪啪啪. #include<iostream> using namespace ...

  10. 设置css属性