Gourmet and Banquet CodeForces
来源:互联网 发布:如何注销知乎账号 编辑:程序博客网 时间:2024/05/21 14:06
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.
Example
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).
每组输入指在[a,b]区间内时间段有某道菜供应,要求要吃到所有的菜,每道花的时间相同,求最长的吃菜时间。
二分找答案,运用贪心策略,按照结束时间排序,从最早结束的开始吃,可以尽可能减少对后面的影响。
#include <string.h>#include <stdio.h>#include <algorithm>using namespace std;int n,t,i,j,low,high,mid,s;int vis[10005];struct node{ int a,b;}di[101];bool cmp(node x,node y){ return x.b<y.b;}int check(int mid){ memset(vis,0,sizeof(vis)); for(i=0;i<n;i++){ if((di[i].b-di[i].a)<mid){ return 0; } t=0; for(j=di[i].a;j<di[i].b;j++){ if(!vis[j]){ t++; vis[j]=1; if(t==mid){ break; } } } if(t<mid){ return 0; } } return 1;}int main(){ while ((scanf("%d",&n))!=EOF){ low=0;high=20010; s=0; for(i=0;i<n;i++){ scanf("%d %d",&di[i].a,&di[i].b); } sort(di,di+n,cmp); while(low<high-1){ mid=(low+high)/2; if(check(mid)){ low=mid; s=mid*n; }else{ high=mid; } } printf("%d\n",s); } return 0;}
- Gourmet and Banquet -CodeForces
- Gourmet and Banquet CodeForces
- CodeForces 589F Gourmet and Banquet 题解
- day4 CodeForces 589F Gourmet and Banquet
- codeforces 589F Gourmet and Banquet
- CodeForces 589F Gourmet and Banquet(二分 + 最大流)
- CodeForces 589F -- F. Gourmet and Banquet (二分 + 贪心)
- CodeForces 589F Gourmet and Banquet(贪心+二分)
- Codeforces 589F Gourmet and Banquet(二分+贪心)
- CodeForces 589F:Gourmet and Banquet(二分+贪心)
- 2015 NEERC F. Gourmet and Banquet
- 文章标题 codeforces 598F :Gourmet and Banquet(贪心+二分)
- CF589F Gourmet and Banquet(二分答案+最大流)
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest F. Gourmet and Banquet(贪心+二分)
- 【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest F】【贪心+ STL-优先队列】 Gourmet and Banquet 最大活动时间
- [二分+线段树构图+网络流]CodeForces 589FGourmet and Banquet
- HUNANOJ -- Gourmet
- poj 3622 Gourmet Grazers
- POJ 1469COURSES
- VHDL中信号与变量的区别及赋值的讨论
- psql -W
- 第K小数(二分)
- LeetCode85 Maximal Rectangle
- Gourmet and Banquet CodeForces
- 匈牙利板子
- 笔试题:如何判断单链表是否存在环
- 升为主程序,总希望总结点什么
- 动态规划-数组最长递增子序列长度
- 从硬币找零问题:看分治/动态规划/贪心算法的区别与联系
- psql中使用\set为某个命令创建别名
- LPC1788 U盘读写及IAP的实现 第二章
- 贪心+线性基 [BeiJing2011]元素