题目链接:http://codeforces.com/problemset/problem/589/F

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
32 41 56 9

Output
6

Input
31 21 21 2

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

题目大意:输入n,代表有n种菜,下面n行代表每种菜上来的时间和下去的时间,要求你每道菜吃的时间一样多,问你最多可以吃多少多久,每一秒只能吃一道菜

思路:并不是自己的思路,自己不知道怎么贪心,大概猜出来是二分求值。。。 贪心的思想是按照每一道菜的端下去的时间从小到大排序,然后从前往后选择就行。。。这是为何呢?

因为我们要做的就是让选择的这道菜对其他菜影响最可能的少,然后我们按照端下去的时间排序,证明我们当前吃的菜端下去的时间是在其他菜端下去之前的,那么我们从前往后找时间是不是对其他菜影响最小呢?  当然是,接下来就看代码了

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e2+10;
const int maxk=1e4+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1]
bool vis[maxk];
int n;
struct dish
{int be,en;
}d[maxn];
bool cmp(const dish a,const dish b)
{return a.en<b.en;
}
bool solve(int mid)
{for(int i=0;i<n;i++){int sum=0;for(int j=d[i].be;j<d[i].en;j++){if(!vis[j]){vis[j]=true;sum++;}if(sum>=mid) break;}if(sum<mid)return false;}return true;
}
int main()
{memset(vis,false,sizeof(vis));int mi=maxk;cin>>n;for(int i=0;i<n;i++){cin>>d[i].be>>d[i].en;mi=min(abs(d[i].en-d[i].be),mi);}sort(d,d+n,cmp);int l=0,r=mi,mid=mi,ans=mid;while(l<=r){memset(vis,false,sizeof(vis));if(solve(mid)){l=mid+1;ans=mid;}elser=mid-1;mid=(l+r)/2;}cout<<ans*n<<endl;return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/9412474.html

F. Gourmet and Banquet(贪心加二分求值)相关推荐

  1. Asia Yokohama Regional Contest 2018 K题 - Sixth Sense(思维加贪心加二分)

    https://codeforces.com/gym/102082 题意 两个人玩n轮游戏,每次两个人各取一个数,并且已知对手的取数顺序,如果你取的数比他大就算赢一轮,求能赢最多轮次的取法,如果有多种 ...

  2. 二分求值(二分适合求答案在两个数之间的题目)

    二分搜索,通过不断缩小解可能存在的范围,从而求得最优解的方法 题目链接:https://vjudge.net/contest/236677#problem/G There are K hours le ...

  3. 算法训练一(贪心、二分)(含解题思路)(下)

    目录 7-15种树(贪心) AC代码: 7-16会场安排问题(贪心) AC代码: 7-17最优合并问题(贪心) AC代码: 7-18简简单单的数学题(位运算 + 哈希表) AC代码: 7-19h148 ...

  4. 算法训练一(贪心、二分)(含解题思路)(上)

    目录 7-1最少失约(贪心) AC代码: 7-2删数问题(贪心) 7-3区间覆盖(贪心) AC代码: 7-7加油站之最小加油次数(贪心+优先队列) AC代码: 7-8求解删数问题(贪心) AC代码: ...

  5. CF-1249 F.Maximum Weight Subset(贪心)

    CF-1249 F.Maximum Weight Subset(贪心) 题目链接 题意 在一棵树上选一些点构成一个集合,满足集合内任意两点的距离大于kkk,求集合的最大权值和 思路 一共200个点,可 ...

  6. hdu 3641 数论 二分求符合条件的最小值数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...

  7. HDU2289_Cup_二分求圆台体积

    题目大意:          一个圆台型的杯子,告诉你杯子的水的体积,要求水的高度. 解题思路:         将水的高度作为x,然后化简出f(x)方程,二分求x,至于圆台的体积公式:v = pi ...

  8. 二分求幂,快速求解a的b次幂

    一个引子 如何求得a的b次幂呢,那还不简单,一个for循环就可以实现! void main(void) {int a, b;int ans = 1;cin >> a >> b; ...

  9. 小A与欧拉路 (树加边求最小权值欧拉路+树的直径)

    链接:https://ac.nowcoder.com/acm/contest/369/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言2621 ...

最新文章

  1. 一致代价搜索_谷歌工程师发布最新技术ScaNN:可实现高效的向量相似性搜索
  2. P3项目全球模板狗血设置之一 --- 发货到成本中心需要输入Customer
  3. 主成分分析(Principal components analysis)-最大方差解释
  4. 爬虫 | urllib入门+糗事百科实战
  5. 使用axios上传照片
  6. STM32学习1之ADC+DMA(使用定时器触发)
  7. 初学者python笔记(内置函数_2)
  8. 【顶】辞职也需要辞得帅,辞得大家都开心,多为将来考虑,辞职不要急,本是好事要办好...
  9. android菜单动画,利用 android studio 制作一个菜单动画
  10. 40岁,将站到哪里(转)
  11. 质疑“扩版=质量下降”——以《中国农学通报》和《安徽农业科学》为例
  12. 如何有逻辑的,简单清晰的回应问题
  13. monkey测试介绍
  14. 卡密社区源码无加密完整版(php源码+mysql脚本)
  15. 乐优商城项目实战系列2
  16. IOS苹果开发者账号封号的规避心得,利用好防关联工具避免APP下架问题
  17. 大学计算机实践教程课后答案,大学计算机基础实践教程习题答案.pdf
  18. 小程序与公众号共用服务器端,小程序和公众号能否共用同一个数据库
  19. python怎么计算圆_python根据圆的参数方程求圆上任意一点的坐标
  20. 又见上海电影节抢票热,电影节不该只有狂欢

热门文章

  1. “C语言” 读书札记(五)之[让我们一起学习汇编吧!(段)]
  2. 【报告分享】数实共生:未来经济白皮书2021-腾讯研究院.pdf(附下载链接)
  3. 【报告分享】抖音短视频营销之“梗”的应用(借梗造梗13招).pdf
  4. 编译成功,但引用之后报错undefined symbol: _ZN10tensorflow7strings6StrCatERKNS0_8AlphaNumE
  5. 『优势特征知识蒸馏』在淘宝推荐中的应用
  6. Leetcode每日一题:1025.divisor-game(除数博弈)
  7. 集成学习—决策树(CART)
  8. 吴恩达机器学习之单变量线性回归实现部分
  9. 神经网络Neural Net背景介绍
  10. python编程:从入门到实践学习笔记-文件和异常