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.

Examples
input

Copy
3
2 4
1 5
6 9
output

Copy
6
input

Copy
3
1 2
1 2
1 2
output

Copy
0
Note

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).

二分+贪心,一般两种情况需要特殊处理,两个时间段有重叠部分,或者两个时间段,一个完全处于另一个范围,即被另一个包含,前一种情况最好先处理前面的,重叠部分则是根据情况来看的,后面那种情况最好先处理被包含的,也就是时间段少的,这里用到贪心思想。然后应该取多大的时间段,可以举,当然是二分来解决,如果是可行的,就让l=mid,如果不行,就让r=mid-1.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 101
using namespace std;
typedef pair<int,int> pa;
int n;
pa p[MAX];
bool vis[];
bool cmp(pa a,pa b) {
return a.second < b.second;
}
bool judge(int k) {
memset(vis,false,sizeof(vis));
for(int i = ;i < n;i ++) {
int d = ;
for(int j = p[i].first;j < p[i].second && d < k;j ++) {
if(!vis[j]) {
d ++;
vis[j] = true;
}
}
if(d < k) return false;
}
return true;
}
int main() {
scanf("%d",&n);
int l = ,r = ,mid;
for(int i = ;i < n;i ++) {
scanf("%d%d",&p[i].first,&p[i].second);
r = min(r,p[i].second - p[i].first);
}
sort(p,p + n,cmp);
while(l < r) {
mid = (l + r + ) / ;
if(judge(mid)) {
l = mid;
}
else {
r = mid - ;
}
}
printf("%d\n",l * n);
}

Codeforces 589F Gourmet and Banquet的更多相关文章

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

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

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

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

  3. 网络流CodeForces. Original 589F:Gourmet and Banquet

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

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

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

  5. CodeForces - 589F —(二分+贪心)

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

  6. Codeforces 1154C Gourmet Cat

    题目链接:http://codeforces.com/problemset/problem/1154/C 题目大意: 主人有一只猫.周一&周四&周日:吃鱼周二&周六:吃兔子周三 ...

  7. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

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

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

  9. Codeforces Round #552 (Div. 3) C. Gourmet Cat (数学,模拟)

    题意:你要带着你的喵咪一起去旅行,你的喵在星期\(1,4,7\)吃喵粮\(x\),在星期\(2,6\)吃喵粮\(y\),在星期\(3,5\)吃喵粮\(z\),你只有\(a\)个\(x\),\(b\)个 ...

随机推荐

  1. 基于SSM的单点登陆05

    springmvc.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  2. windows10下Python如何设置环境变量

    1.右击“我的电脑”,选择“属性”, 2.选择“高级系统设置”, 3.选择“环境变量”, 4.在“系统变量”中选中“Path”,再点“新建”.(Python.Scripts两个目录都要加,只加Pyth ...

  3. 【Java并发】Java中的原子操作类

    综述 JDK从1.5开始提供了java.util.concurrent.atomic包. 通过包中的原子操作类能够线程安全地更新一个变量. 包含4种类型的原子更新方式:基本类型.数组.引用.对象中字段 ...

  4. quartz(7)-源码分析

    定时器启动 上图通过spring加载quartz <bean id="scheduler" class="org.springframework.schedulin ...

  5. spark学习12(spark架构原理)

    spark采用的是主从式的架构,主节点叫master,从节点是worker Driver 我们编写的spark就在Driver上,由driver进程执行. Driver是spark集群的节点之一,或你 ...

  6. Substring with Concatenation of All Words, 返回字符串中包含字符串数组所有字符串元素连接而成的字串的位置

    问题描述:给定一个字符数组words,和字符串s,返回字符数组中所有字符元素组成的子串在字符串中的位置,要求所有的字符串数组里的元素只在字符串s中存在一次. 算法分析:这道题和strStr很类似.只不 ...

  7. docker 跨主机网络:overlay 简介

    简介 docker 在早前的时候没有考虑跨主机的容器通信,这个特性直到 docker 1.9 才出现.在此之前,如果希望位于不同主机的容器能够通信,一般有几种方法: 使用端口映射:直接把容器的服务端口 ...

  8. Kubernetes源码之旅:从kubectl到API Server

    概述: Kubernetes项目目前依然延续着之前爆炸式的扩张.急需能够理解Kubernetes原理并且贡献代码的软件开发者.学习Kubernetes源码并不容易.Kubernetes是使用相对年轻的 ...

  9. mysql 程序编写实例

    DELIMITER $$ drop trigger if exists `updata_depart_lead_name`;-- 新建触发器 ---- 功能:在更新部门的领导编号是,自动填写部门领导名 ...

  10. django-simple-captcha 使用 以及添加动态ajax刷新验证

    参考博客:http://blog.csdn.net/tanzuozhev/article/details/50458688?locationNum=2&fps=1 参考博客:http://bl ...