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. s标签可以if elseif else

    首先引用s标签: <%@ taglib prefix="s" uri="/struts-tags" %> 使用s标签进行if elseif else ...

  2. java csv - 读写及其操作.

    今天帮同学处理数据, 主要是从1w多条记录中随机获取8k条, 然后再从8k条记录中随机获取2k条记录. 最后将2k条记录中随机分成10组,使得每组的记录都不重复. 下面将我的代码都贴上来, 好以后处理 ...

  3. 在.Net中进行跨线程的控件操作(上篇:Control.Invoke)

    本文的重点在于介绍如何在多线程编程中,从非UI线程上访问界面中的控件.有过多线程编程经验的人都知道,当我们在非UI线程上试图给一个界面中的控件赋值的时候,比如说label的Text属性,系统会抛出一个 ...

  4. 一个简单的web服务器例子

    一个简单的web容器小例子,功能十分简单,只能访问静态资源,对于新手来说还是有一定的意义.主要分三个类 1.server类:主要功能开启socketServer,阻塞server,接收socket访问 ...

  5. oc 获取当前时间

    //获取当前日期 NSDate* date = [NSDate date]; NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init]; ...

  6. 简单的背包变形HDU1203,HDU2955

    今天一直在写背包,不过中间停了一段时间在写shell. 一直在做01背包.今天做了这两题很相似的背包 首先是HDU1203 Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要 ...

  7. Data Abstraction

    What is an object? (Page 238) In C++, an object is just a variable, and the purest definition is &qu ...

  8. 不错的轮播插件flexslider

    http://flexslider.woothemes.com/ $('.flexslider').flexslider({ animation:'slide', //滑动效果:翻页效果,默认为fad ...

  9. interview:about Oracle表空间

    Oracle表空间 SQL Server数据库与Oracle数据库之间最大的区别要属表空间设计.Oracle数据库开创性地提出了表空间的设计理念,这为Oracle数据库的高性能做出了不可磨灭的贡献.可 ...

  10. Linux脚本(二)

    1.for循环以及加法的使用 portStr=`lsof -i:56801 | head -2`count=0for str in `lsof -i:56801 | head -2`do ((coun ...