博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 589F Gourmet and Banquet
阅读量:5273 次
发布时间:2019-06-14

本文共 3781 字,大约阅读时间需要 12 分钟。

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 
#include
#include
#include
#define MAX 101using namespace std;typedef pair
pa;int n;pa p[MAX];bool vis[10001];bool cmp(pa a,pa b) { return a.second < b.second;}bool judge(int k) { memset(vis,false,sizeof(vis)); for(int i = 0;i < n;i ++) { int d = 0; 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 = 0,r = 10000,mid; for(int i = 0;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 + 1) / 2; if(judge(mid)) { l = mid; } else { r = mid - 1; } } printf("%d\n",l * n);}

 

转载于:https://www.cnblogs.com/8023spz/p/9853397.html

你可能感兴趣的文章
Python开发的10个小贴士
查看>>
bzoj:3616: War
查看>>
qrcode length overflow (1632>1056)--qrcode.js使用过程中二维码长度溢出解决办法
查看>>
我踩过的听过的那些坑
查看>>
CSS 制作3D旋转视频
查看>>
JQ全选反选
查看>>
sql语句中in和exists的区别
查看>>
python-处理文件
查看>>
eclipse6.5 自动注册代码
查看>>
spring配置文件比较全的约束
查看>>
Sqlit--学习教程(附加数据库)
查看>>
9月2日笔记
查看>>
文件拷贝 上传下载 输入流输出流个人小结,仅供自己使用
查看>>
OptimalSolution(2)--二叉树问题(2)BST、BBT、BSBT
查看>>
342.4的幂
查看>>
Anroid 搭建Maven私服(Android Studio)
查看>>
Beta 冲刺 (2/7)
查看>>
PHP开发环境配置系列(三)-项目源码映射
查看>>
腾讯官方教程
查看>>
React跨域问题解决
查看>>