CodeForces 589F
来源:互联网 发布:软件设计师报名入口 编辑:程序博客网 时间:2024/04/29 10:32
A gourmet came into the banquet hall, where the cooks suggestedn 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 timeai andbi (in seconds from the beginning of the banquet) — when the cooks will bring thei-th dish into the hall and when they will carry it out (ai < bi). For example, ifai = 10 andbi = 11, then thei-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 forthe 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?
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. Thei-th line contains two integers ai and bi (0 ≤ ai < bi ≤ 10000) — the moments in time when thei-th dish becomes available for eating and when thei-th dish is taken away from the hall.
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.
32 41 56 9
6
31 21 21 2
0
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道菜,每道菜吃相同的时间,求总时间的最大值。
将每个区间的右端点排序,使用贪心思想,保证最优。
从0到区间最大长度开始二分。
#include<cstdio>#include<string.h>#include<algorithm>using namespace std;int n;int ans;int vis[10005];struct node{ int a,b; bool operator < (const node& tmp) const { return b<tmp.b||(b==tmp.b&&a<tmp.a); }}dish[105];bool fun(int m){ memset(vis,0,sizeof(vis)); int i; for(i=0;i<n;i++) { if(dish[i].b-dish[i].a<m) return false; int cur=0; int j; for(j=dish[i].a;j<dish[i].b;j++) { if(!vis[j]) { cur++; vis[j]=1; if(cur==m) break; } } if(cur<m) return false; } return true;}int main(){ while(scanf("%d",&n)!=EOF) { int i; int right=0,left=0; ans=0; for(i=0;i<n;i++) { scanf("%d%d",&dish[i].a,&dish[i].b); if(dish[i].b-dish[i].a>right) right=dish[i].b-dish[i].a; } sort(dish,dish+n); while(left<=right) { int mid=(left+right)>>1; if(fun(mid)) { left=mid+1; ans=max(ans,mid); } else right=mid-1; } printf("%d\n",ans*n); } return 0;}
- codeforces 589F(二分)
- CodeForces 589F
- CodeForces 589F 二分答案
- Codeforces 659F F
- CodeForces 589F -- F. Gourmet and Banquet (二分 + 贪心)
- CodeForces 589F Gourmet and Banquet 题解
- CodeForces 589F 究竟能吃多久?
- day4 CodeForces 589F Gourmet and Banquet
- codeforces 589F Gourmet and Banquet
- Codeforces 234 F. Fence
- 【Codeforces 500F】Dp
- codeforces 234F - Fence
- F-Logo Turtle codeforces
- 【CODEFORCES】 F. Ant colony
- Codeforces 567F
- CodeForces 241F Race
- Codeforces 611F 思维
- Codeforces 542F 构造
- JVM中的直接引用和符号引用
- codeforces 700C. Break Up 枚举
- 65. Valid Number
- Codeforces Round #368 (Div. 2) E. Garlands(查询矩形内的和)
- Java基础-方法区以及static的内存分配图
- CodeForces 589F
- Educational Codeforces Round 1C. Nearest vectors
- ubuntu下部署Django项目(成功)
- DPDK安装
- Linux(CentOS)中常用软件安装,使用及异常——MySQL, VmTools
- Unique Binary Search Trees II
- Spring Boot整合MyBatis
- JVM深入与项目中应用--(一)JVM理解
- Glide加载图片类型