网络流CodeForces. Original 589F:Gourmet and Banquet
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.
Sample Input
3
2 4
1 5
6 9
6
3
1 2
1 2
1 2
0
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=,M=,INF=;
int n,m,cnt,fir[N],fron[N],nxt[M],to[M],cap[M];
int path[N],gap[N],dis[N],q[N],front,back;
struct Net_Flow{
void Init(){
memset(fir,,sizeof(fir));
memset(gap,,sizeof(gap));
memset(dis,,sizeof(dis));
front=back=cnt=;
}
void addedge(int a,int b,int c){
nxt[++cnt]=fir[a];to[fir[a]=cnt]=b;cap[cnt]=c;
nxt[++cnt]=fir[b];to[fir[b]=cnt]=a;cap[cnt]=;
}
bool BFS(int S,int T){
q[back++]=T;dis[T]=;
while(front<back){
int x=q[front++];
for(int i=fir[x];i;i=nxt[i])
if(!dis[to[i]])dis[q[back++]=to[i]]=dis[x]+;
}
return dis[S];
}
int ISAP(int S,int T){
if(!BFS(S,T))return ;
for(int i=S;i<=T;i++)gap[dis[i]]+=;
for(int i=S;i<=T;i++)fron[i]=fir[i];
int ret=,f,p=S,Min;
while(dis[S]<=T+){
if(p==T){f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}ret+=f;p=T;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
}
for(int &i=fron[p];i;i=nxt[i])
if(cap[i]&&dis[p]==dis[to[i]]+){
path[p=to[i]]=i;break;
}
if(!fron[p]){
if(!--gap[dis[p]])break;Min=T+;
for(int i=fir[p];i;i=nxt[i])
if(cap[i])Min=min(Min,dis[to[i]]);
gap[dis[p]=Min+]+=;fron[p]=fir[p];
if(p!=S)p=to[path[p]^];
}
}
return ret;
}
}isap;
int l[N],r[N],hsh[N];
int S,T,lo=,hi,cntx;
bool Check(int x){
isap.Init();
S=;T=n+cntx+;
for(int i=;i<=n;i++)
isap.addedge(S,i,x);
for(int i=;i<cntx;i++)
isap.addedge(i+n,T,hsh[i+]-hsh[i]);
for(int i=;i<=n;i++)
for(int j=l[i];j!=r[i];j++)
isap.addedge(i,n+j,INF);
return isap.ISAP(S,T)==n*x;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&l[i],&r[i]);
hi=max(hi,r[i]-l[i]);
hsh[++cntx]=l[i];
hsh[++cntx]=r[i];
}
sort(hsh+,hsh+cntx+);
cntx=unique(hsh+,hsh+cntx+)-hsh-;
for(int i=;i<=n;i++){
l[i]=lower_bound(hsh+,hsh+cntx+,l[i])-hsh;
r[i]=lower_bound(hsh+,hsh+cntx+,r[i])-hsh;
}
while(lo<=hi){
int mid=(lo+hi)>>;
if(Check(mid))lo=mid+;
else hi=mid-;
}
printf("%d\n",hi*n);
return ;
}
网络流CodeForces. Original 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 ...
- 【CodeForces 589F】Gourmet and Banquet(二分+贪心或网络流)
F. Gourmet and Banquet time limit per test 2 seconds memory limit per test 512 megabytes input stand ...
- F. Gourmet and Banquet(贪心加二分求值)
题目链接:http://codeforces.com/problemset/problem/589/F A gourmet came into the banquet hall, where the ...
- codeforces #541 D. Gourmet choice(拓扑+并查集)
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...
- CodeForces 321C Ciel the Commander
Ciel the Commander Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...
- CodeForces 221D Little Elephant and Array
Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...
- Codeforces 121A Lucky Sum
Lucky Sum Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...
- CodeForces 551E GukiZ and GukiZiana
GukiZ and GukiZiana Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...
随机推荐
- artTemplate模板引擎的源码拜读
最初接触的模板引擎还是基于node的ejs,当时觉得很神奇原来还可以这么玩,后来随着学习的深入,使用过jade,doT等,当然还有一些比较火的诸如juicer.underscore还没有深入接触,直到 ...
- ionic 项目分享【转】No.3
写在文章前:由于最近研究ionic框架,深感这块的Demo寥寥可数,而大家又都藏私,堂堂天朝,何时才有百家争鸣之象,开源精神吾辈当仁不让! 原文地址暂时忘记了 ,如果有知道的麻烦在评论处帮忙说一下 , ...
- 使用LuaInterface遇到的编码问题
今天使用LuaInterface加载脚本时忽然报“未知字符”错误信息!于是检查文件编码 将其修改为“US ASCII” 就好了.
- [FTP] FTPOperater--FTP操作帮助类 (转载)
点击下载 FTPOperater.zip 这个类是关于FTP的一些操作的1.连接FTP服务器 2.上传3.下载4.删除文件5.获取当前目录下明细(包含文件和文件夹) 6.获取FTP文件列表(包括文件 ...
- Http协议、线程、线程池
Socket模拟服务端运行代码: 1:启动服务端监听的服务,并接受客户端的连接 1.1 创建Socket Socket listenSocket=new Socket(AddressFamily.In ...
- 一步一步建MVC
http://www.cnblogs.com/yuangang/p/5569518.html
- 利用反射把查询到的Table、Reader转换成List、Model
菜鸟一枚,入园已有两年三个月,这还是第一次写博客,请各位大神斧正. 这是我写的一个工具类,通常我们从数据库查询到一个 DataReader 或者是 一个 Table , 想要转换成 一个 lis ...
- 关于java中for和foreach循环
for循环中的循环条件中的变量只求一次值!具体看最后的图片 foreach语句是java5新增,在遍历数组.集合的时候,foreach拥有不错的性能. foreach是for语句的简化,但是forea ...
- 【转】NHibernate入门教程
开源框架完美组合之Spring.NET + NHibernate + ASP.NET MVC + jQuery + easyUI 中英文双语言小型企业网站Demo 摘要: 热衷于开源框架探索的我发现A ...
- Java之加密(信息摘要)工具类(依赖:java.security.MessageDigest或org.apache.commons.codec.digest.DigestUtils)
依赖于java.security.MessageDigest,支持MD5,SHA-1,SHA-256 import java.security.MessageDigest; import java.s ...