【CodeForces 589F】Gourmet and Banquet(二分+贪心或网络流)
2 seconds
512 megabytes
standard input
standard output
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?
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 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.
3
2 4
1 5
6 9
6
3
1 2
1 2
1 2
0
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).
每秒可以吃一种菜,每个菜有个可吃的时间区间,要求每种菜吃一样长时间,最多吃多久。
先二分这个时间。
然后贪心:按结束时间排序,越早结束的越先吃,从可以吃的最早时间开始吃。影响最小。
或者网络流:源点到每个菜建边,权为菜的时间,每个菜到可以吃的每个时间点建边,权为1,每个时间点到汇点建边,权为1。如果最大流为当前二分值,则该时间不比答案小。
#include <cstdio>
#include <algorithm>
#include <cstring>
#define inf 0x3f3f3f3f
#define N 105
using namespace std;
struct dish{
int l,r;
}d[N];
int n,vis[];
int cmp(dish a,dish b){
return a.r<b.r;
}
int solve(int len){
memset(vis,,sizeof vis);
for(int i=;i<=n;i++){
int x=;
for(int j=d[i].l;j<d[i].r;j++){
if(vis[j]==){
vis[j]=i;
x++;
if(x==len)break;
}
}
if(x<len)return ;
}
return ;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&d[i].l,&d[i].r);
sort(d+,d++n,cmp);
int l=,r=;
while(l<r-){
int m=(l+r)>>;
if(solve(m))l=m;
else r=m;
}
printf("%d",l*n);
}
【CodeForces 589F】Gourmet and Banquet(二分+贪心或网络流)的更多相关文章
- codeforces 589F. Gourmet and Banquet  二分+网络流
		
题目链接 给你n种菜, 每一种可以开始吃的时间不一样, 结束的时间也不一样. 求每种菜吃的时间都相同的最大的时间.时间的范围是0-10000. 看到这个题明显可以想到网络流, 但是时间的范围明显不允许 ...
 - Codeforces 589F Gourmet and Banquet
		
A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...
 - F. Gourmet and Banquet(贪心加二分求值)
		
题目链接:http://codeforces.com/problemset/problem/589/F A gourmet came into the banquet hall, where the ...
 - codeforces 613B B. Skills(枚举+二分+贪心)
		
题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
 - CodeForces 377B---Preparing for the Contest(二分+贪心)
		
C - Preparing for the Contest Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
 - Codeforces C Match Points(二分贪心)
		
题目描述: Match Points time limit per test 2 seconds memory limit per test 256 mega bytes input standard ...
 - codeforces 1251D Salary Changing (二分+贪心)
		
(点击此处查看原题) 题意分析 一共有s元钱,要用这些钱给n个人发工资,发给每个人的工资si有最少和最多限制 si ∈[li,ri],在发给n个人的总工资小于s的情况下,要求发给n个人中的工资的中位数 ...
 - Codeforces Round #262 (Div. 2) 二分+贪心
		
题目链接 B Little Dima and Equation 题意:给a, b,c 给一个公式,s(x)为x的各个位上的数字和,求有多少个x. 分析:直接枚举x肯定超时,会发现s(x)范围只有只有1 ...
 - CodeForces 551C - GukiZ hates Boxes - [二分+贪心]
		
题目链接:http://codeforces.com/problemset/problem/551/C time limit per test 2 seconds memory limit per t ...
 
随机推荐
- Eclipse tooltip变黑的修正
 - JVM监测&工具[转]
			
通过工具及Java api来监测JVM的运行状态, 需要监测的数据:(内存使用情况 谁使用了内存 GC的状况) 内存使用情况--heap&PermGen @ 表示通过jmap –heap pi ...
 - Java daemon thread 守护线程
			
守护线程与普通线程写法上基本么啥区别,在启动线程前, 调用线程对象的方法setDaemon(true),则可以将其设置为守护线程. 守护线程使用的情况较少,但并非无用,举例来说,JVM的垃圾回收.内存 ...
 - Android项目,从web上取下汉字,中文部分乱码
			
Android项目,从web上取下汉字,中文部分乱码. 常见问题,搜索一下,网上有很多办法解决.如果还没有试过这个办法,可以尝试一下. BufferedReader in = new Buffered ...
 - 傅盛:如何快慢“炼”金山?(转)
			
原文地址:http://www.huxiu.com/article/16052/1.html 一直以来,金山都不是一家"大公司",从前不是,现在也不是. 能够掰着指头数完腾讯六大事 ...
 - U3D sorting layer, sort order, order in layer, layer深入辨析
			
1,layer是对游戏中所有物体的分类别划分,如UIlayer, waterlayer, 3DModelLayer, smallAssetsLayer, effectLayer等.将不同类的物体划分到 ...
 - 增强for循环(forearch)
			
增强for循环是为了简化在遍历数组需要先获得数组的长度或者在遍历集合中的元素的时候需要使用迭代器的操作. 引入时间:JDK1.5 语法格式: for(数据类型 变量 :需要迭代的数组或者集合){ } ...
 - opencv5-objdetect之级联分类器
			
这是<opencv2.4.9tutorial.pdf>的objdetect module的唯一一个例子. 在opencv中进行人脸或者人眼 或者身体的检测 首先就是训练好级联分类器,然后就 ...
 - SQL 批量修改表结构
			
项目中发现一批语言表的某个字段设的值太小了需要增大,因为涉及到很多张表,所以采用游标一张张的处理. 代码很简单 ) ) DECLARE LangTable CURSOR FOR SELECT name ...
 - 在项目中代替DevExpress(一)
			
从Delphi时代开始一直都是DevExpress系列控件的忠实用户,到现在已经有10多个年头了.DevExpress里面的控件基本从头到尾都用过一次,而且也开发过很多基于DevExpress的子控件 ...