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
Input
3
2 4
1 5
6 9
Output
6
Input
3
1 2
1 2
1 2
Output
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).
题意:有N到菜,每道菜有上菜的时间(x,y),想每道菜都能吃到,且时间是整数的,比如(10,11)能吃一秒,且区间内的点只能用一次,现在求N道菜都能吃到的前提下,吃的最长时间。
分析:显然这得用到贪心,而贪心哪个点呢,显然应该先贪右端点然后左端点,因为以右端点为第一关键点的话,对后面的区间的影响最小。然后二分时间,求出最长能吃的时间,乘以菜的数量就是答案。
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node {
int x,y;
bool operator <( const node &t )const {
return (y<t.y||y==t.y && x<t.x);
}
};
node a[105];
int n;
int flag[10005];
int cnt;
//二分
bool judge (int x){
memset (flag,0,sizeof(flag));
for (int i=0;i<n;i++){
cnt=0;
for (int j=a[i].x+1;j<=a[i].y;j++){
if (!flag[j]){
cnt++;
flag[j]=1;
// 如果这个区间吃菜的时间大于或等于x,说明每道菜吃的时间大于x,
//就可以跳出这个循环了
if (x==cnt)break;
}
}
//判断是否每个区间吃菜的时间都大于x,如果没有,则说明x大了,得较小x
if (cnt<x)return false;
}
return true;//如果每个区间吃的时间都大于x说明,吃菜的时间还可以再大;
}
int main ()
{
while (scanf ("%d",&n)!=EOF){
for (int i=0;i<n;i++){
scanf ("%d%d",&a[i].x,&a[i].y);
}
sort (a,a+n);
/*for (int i=0;i<n;i++){
printf ("%d %d\n",a[i].x,a[i].y);
}*/
int lo=1,hi=10001;
int mi;
while (lo<hi-1){
//mi=(hi-lo)>>1+lo;
mi=(lo+hi)/2;
if (judge(mi)) lo=mi;
else hi=mi;
}
if (judge(lo))printf ("%d\n",lo*n);
else printf ("0\n");
}
return 0;
}