F. Gourmet and Banquet(贪心加二分求值)

题目链接: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
3
2 4
1 5
6 9
Output
6
Input
3
1 2
1 2
1 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;
        }
        else
            r=mid-1;
        mid=(l+r)/2;
    }
    cout<<ans*n<<endl;
    return 0;
}
当初的梦想实现了吗,事到如今只好放弃吗~
原文地址:https://www.cnblogs.com/caijiaming/p/9412474.html