【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 ...
随机推荐
- java 22 - 19 多线程之生产者和消费者的代码优化
在之前,是把生产者录入数据和消费者获取数据的所有代码都分别写在各自的类中. 这样不大好 这次把生产者和消费者部分关键代码都写入资源类中: package zl_Thread; public class ...
- luogu1328[NOIP2014 T1]生活大爆炸版石头剪刀布
题目描述 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负.在<生活大爆炸>第二季第8 集中出现了一种石头剪刀布的升级版游戏. 升级版游戏在传统的 ...
- Android 下载网络图片保存到本地
通过网络地址获取网络图片,点击下载将图片显示出来,然后点击图片将图片保存到本地. 首先需要在manifest上添加一些权限: <!-- 访问网络的权限 --> <uses-permi ...
- css的小问题总结
1.居中问题 比如让宽度为60%的<div class="box">居中,可以再.box里面设置margin:auto 2.高度固定的div里面有两个子类div且高度和 ...
- 为什么做前端要做好SEO
我就挑干货说啦SEO可能听起来很高大上,其实翻译成中文就是"搜索引擎优化",它只是通过一定的方法在网站内外发布文章.交换连接等,最终达到某个关键词在搜索引擎上获得好的排名. 我有幸 ...
- hadoop怎么读?怎么发音
hadoop不是一个英文单词,是作者发明的词,hadoop名称来源作者小孩的一个}h毛填充黄色大象玩具. 它的发音是:[hædu:p]
- js 增删改查方法
push() 向数组的末尾添加一个或多个元素 pop() 删除数组内部并返回数组的最后一个元素 shift() 把数组内部的第一个元素从其中删除,并返回第一个元素的值 unshift() 向数组外部的 ...
- 动画制作库tween样例学习
mark: https://www.npmjs.com/package/tween
- Openjudge 1.3-04 垂直直方图
04:垂直直方图 查看 总时间限制: 1000ms 内存限制: 65536kB 描述 输入4行全部由大写字母组成的文本,输出一个垂直直方图,给出每个字符出现的次数.注意:只用输出字符的出现次数,不用输 ...
- NET代码重构
记一次.NET代码重构 好久没写代码了,终于好不容易接到了开发任务,一看时间还挺充足的,我就慢慢整吧,若是遇上赶进度,基本上直接是功能优先,完全不考虑设计.你可以认为我完全没有追求,当身后有鞭子使 ...