zoukankan      html  css  js  c++  java
  • 【CodeForces 589F】Gourmet and Banquet(二分+贪心或网络流)

    F. Gourmet and Banquet
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    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).

    每秒可以吃一种菜,每个菜有个可吃的时间区间,要求每种菜吃一样长时间,最多吃多久。

    先二分这个时间。

    然后贪心:按结束时间排序,越早结束的越先吃,从可以吃的最早时间开始吃。影响最小。

    或者网络流:源点到每个菜建边,权为菜的时间,每个菜到可以吃的每个时间点建边,权为1,每个时间点到汇点建边,权为1。如果最大流为当前二分值,则该时间不比答案小。

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #define inf 0x3f3f3f3f
    #define N 105
    using namespace std;
    struct dish{
    int l,r;
    }d[N];
    int n,vis[10005];
    int cmp(dish a,dish b){
        return a.r<b.r;
    }
    int solve(int len){
        memset(vis,0,sizeof vis);
        for(int i=1;i<=n;i++){
            int x=0;
            for(int j=d[i].l;j<d[i].r;j++){
                if(vis[j]==0){
                    vis[j]=i;
                    x++;
                    if(x==len)break;
                }
            }
            if(x<len)return 0;
        }
        return 1;
    }
    int main(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d%d",&d[i].l,&d[i].r);
        sort(d+1,d+1+n,cmp);
        int l=0,r=10001;
        while(l<r-1){
            int m=(l+r)>>1;
            if(solve(m))l=m;
            else r=m;
        }
        printf("%d",l*n);
    }

      

  • 相关阅读:
    C#.NET 以上超大文件上传和断点续传服务器的实现
    ASP.NET 以上超大文件上传和断点续传服务器的实现
    JSP 以上超大文件上传和断点续传服务器的实现
    Java 以上超大文件上传和断点续传服务器的实现
    4GB以上超大文件上传和断点续传服务器的源码
    4GB以上超大文件上传和断点续传服务器的代码
    4GB以上超大文件上传和断点续传服务器的实现
    hdu 1013 Digital Roots
    hdu 1012 u Calculate e
    hdu 1011 树形dp
  • 原文地址:https://www.cnblogs.com/flipped/p/5815593.html
Copyright © 2011-2022 走看看