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

Input
3
2 4
1 5
6 9
Output
6
Input
3
1 2
1 2
1 2
Output
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的更多相关文章

  1. codeforces 589F. Gourmet and Banquet 二分+网络流

    题目链接 给你n种菜, 每一种可以开始吃的时间不一样, 结束的时间也不一样. 求每种菜吃的时间都相同的最大的时间.时间的范围是0-10000. 看到这个题明显可以想到网络流, 但是时间的范围明显不允许 ...

  2. Codeforces 589F Gourmet and Banquet

    A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...

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

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

  4. F. Gourmet and Banquet(贪心加二分求值)

    题目链接:http://codeforces.com/problemset/problem/589/F A gourmet came into the banquet hall, where the ...

  5. codeforces #541 D. Gourmet choice(拓扑+并查集)

    Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...

  6. CodeForces 321C Ciel the Commander

    Ciel the Commander Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...

  7. CodeForces 221D Little Elephant and Array

    Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...

  8. Codeforces 121A Lucky Sum

    Lucky Sum Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...

  9. CodeForces 551E GukiZ and GukiZiana

    GukiZ and GukiZiana Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...

随机推荐

  1. 认识javascript作用域

    JavaScript的作用域链 这是一个非常重要的知识点了,了解了JavaScript的作用域链的话,能帮助我们理解很多‘异常’问题. 下面我们来看一个小例子,前面我说过的声明提前的例子. var n ...

  2. 为当前的div 动态添加一个样式

    $("#target").addClass("newClass");

  3. 1. 连接字符串的创建 - Lazy.Framework从零开始设计自己的ORM架构

    开发初衷 注册了博客园已经有几个月了,却从来都没有上来过,本人大概从2010年开始就开始做.NET 方向的开发. 这个是我在博客园发布的第一个帖子. 主要就是说说最近在写的一个ORM架构. 本人接触的 ...

  4. 错误:Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 拒绝访问。

    最近,把一个网站部署到另一个服务器上,网站中一个功能word转pdf报下面错误: 在此附上解决方案: 方法1:配置Web.Config文件,在每次请求时模拟本地系统的账户. 具体操作:在Web.Con ...

  5. iOS相关,过年回来电脑上的证书都失效了,解决方法。

    今天发了个问题,就是关于电脑上的证书都失效的问题,就这个问题的解决方法如下:https://segmentfault.com/q/1010000004433963 1,按照链接下载,https://d ...

  6. WCF 无法生成 client

    在MVC中调用WCF 总是没有client 后来在网上查找原因,去掉Reuse type in referrenced assenbiles ,就可以生成代理代码.

  7. JS 同源策略

    对于任何基于WEB的应用,最重要的就是安全性.JS中有各种安全检查以防止恶意脚本攻击你的机器,其中一些特定的安全手段在各种浏览器中都有采用.如:Mozilla有个完全独特的完全模型,涉及到了签署脚本和 ...

  8. 3D Game Programming with directx 11 习题答案 8.3

    第八章 第三题 1.将flare.dds和flarealpha.dds拷贝到工程目录 2.创建shader resource view HR(D3DX11CreateShaderResourceVie ...

  9. 读书笔记之 - javascript 设计模式 - 接口、封装和链式调用

    javascript 采用设计模式主要有下面的三方面原因: 可维护性:设计模式有助于降低模块之间的耦合程度.这使代码进行重构和换用不同的模块变得容易,也使程序员在大型项目中合作变得容易. 沟通:设计模 ...

  10. 浅谈PHP神盾的解密过程

    我们来做第一步解密处理吧. PS: 这只是我的解密思路,与大家分享一下,也许你有更好的方法还望分享 <?php $str = file_get_contents("1.php" ...