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. node express

    在某QQ群里,发现大家都在搞node,为了不被out,这周主要研究了一下,还挺高大上. 参考了下资料,适合初学者学习. Node和NPM的安装够便捷了,不细说...有几点基础顺手提一下: 安装命令中的 ...

  2. git 分布式版本控制了解

    今天也来了解一下这个版本控制神器,下面了解一些词语的意思 先说集中式版本系统,版本库是集中放在中央服务器的,干活的时候,都是用自己的电脑,从中央处理器取得最新的版本,干完活后,在把自己的活推送给服务器 ...

  3. JS事件监听 JS:attachEvent和addEventListener 使用方法

    attachEvent与addEventListener区别适应的浏览器版本不同,同时在使用的过程中要注意attachEvent方法          按钮onclickaddEventListene ...

  4. jQuery 遍历后代

    后代是子.孙.曾孙等等. 通过 jQuery,您能够向下遍历 DOM 树,以查找元素的后代. 向下遍历 DOM 树 下面是两个用于向下遍历 DOM 树的 jQuery 方法: children() f ...

  5. java_设计模式_迭代器模式_Iterator Pattern(2016-08-12)

    迭代子(Iterator)模式又叫游标(Cursor)模式,是对象的行为模式. 定义:提供一种方法访问一个容器对象中各个元素,而又不暴露该对象的内部细节. 类型:行为类模式 类图: 如果要问java中 ...

  6. WinpCap 使用线程发数,明明发了,返回值0是OK的啊,怎么抓包看不到,难道不支持多线程。。。

    if (!m_adapterHandle){    return false;}int rst = pcap_sendpacket((pcap_t*)m_adapterHandle,data ,dat ...

  7. CentOS 6.4 64位 搭建MySQL-Cluster 7.3.8 集群

    准备:1.下载 MySQL-Cluster-gpl-7.3.8-1.el6.x86_64.rpm-bundle.tar 下载地址: http://mirrors.sohu.com/mysql/MySQ ...

  8. details标签和summary标签

    代码: <details open="open"> <summary>幸福度调查结果</summary> <p>对人们幸福度的调查& ...

  9. CI源码引用使用--php引用demo,静态变量和引用关系

    CI源码引用使用在Common.php中,加载配置和类的方法 function &test() {     static $a = '';     if (!$a) {         $a ...

  10. js获取上传文件信息并及时查看

    <form id="picForm" name="picForm"  method="post" enctype="mult ...