CodeForces 589F 究竟能吃多久?
来源:互联网 发布:ae mac 渲染快捷键 编辑:程序博客网 时间:2024/03/29 21:51
题目:
Description
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
32 41 56 9
6
31 21 21 2
0
Hint
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).
这个题目,也是学长说用贪心可以做之后,才做出来的。
虽然感觉和最佳教室安排问题好像,不过还是没想到,贪心策略居然是一模一样的,直接取结束时间最早的就可以了。
当然了,要排除掉吃的时间已经足够的菜。
深深感觉到自己还太弱了。
一开始思路很不清晰,最后才发现除了结点数组之外,不需要其他数组了。
而且ok函数直接用for循环来写比用递归简单多了。
代码:
#include<iostream>using namespace std;struct node//d表示时间{int a;int b;int d;};bool ok(int m, node *p, int n)//m表示时间{if (m == 0)return true;for (int i = 0; i < n; i++)p[i].d = m;for (int k = 0; k < 10000; k++)//函数ok是非递归的,主要就是这个循环{int end = 10001, key = -1;//最后一次修改是把10000改成了10001for (int i = 0; i < n; i++){if (p[i].a <= k && p[i].b>k && p[i].d>0 && end > p[i].b)/////////////////////贪心策略//////////{end = p[i].b;key = i;}}if (key >= 0)p[key].d--;}for (int i = 0; i < n; i++)if (p[i].d)return false;return true;}int main(){int n;while (cin >> n){node *p = new node[n];int dif = 10000;for (int i = 0; i < n; i++){cin >> p[i].a >> p[i].b;if (dif>p[i].b - p[i].a)dif = p[i].b - p[i].a;}int low = 0, high = dif;int mid;while (low +1 < high)//二分查找答案{mid = (high + low) / 2;if (ok(mid,p,n))low = mid;else high = mid - 1;}if (ok(high, p, n))cout << high*n << endl;else cout << low*n << endl;}return 0;}
- CodeForces 589F 究竟能吃多久?
- 从事软件行业究竟能有多久的工作生命力?
- 计算机究竟还能火多久
- 科学家总结出寿命公式,想知道你究竟能活多久吗?那就算一下吧
- 视频网站试水正版影视剧同步播出 免费大餐能吃多久
- codeforces 589F(二分)
- CodeForces 589F
- 看看能坚持多久
- 多久能学会编程
- CodeForces 589F 二分答案
- 算算你能活多久?
- 看看你能活多久!
- PV还能支撑多久
- 程序人生能走多久
- 面试,你能坚持多久?
- 程序员到底能做多久
- 我还能坚持多久?
- 试看自己能坚持多久
- Ubuntu软件下载速度慢解决
- AtCoder Regular Contest 058 C-こだわり者いろはちゃん / Iroha's Obsession
- windows 重装系统 声音不能播放怎么办
- Qt之正则表达式
- SpringSecurity从数据库中获取url资源地址和对应的权限信息
- CodeForces 589F 究竟能吃多久?
- Avro:RPC例子
- unsigned和signed的转换
- 编码规范(华为)
- 垂直进度条VerticalSeekBar
- shell开发入门,第一个shell
- FragmentPagerAdapter刷新数据原理分析与解决
- CSU-ACM2016暑假集训训练1-二分搜索-C
- 《代码大全》读书笔记及随想(day03)