• poj--2376 Cleaning Shifts


    Description

    Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T. 

    Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval. 

    Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.

    Input

    * Line 1: Two space-separated integers: N and T 

    * Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.

    Output

    * Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

    Sample Input

    3 10
    1 7
    3 6
    6 10

    Sample Output

    2

    Hint

    This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 

    INPUT DETAILS: 

    There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10. 

    OUTPUT DETAILS: 

    By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.
     
    题目大意:找到最少的奶牛完成区间的任务。
    题解:本题运用贪心法,先对奶牛进行排序,先开始的在前面,同时开始的先结束的放前面,然后比较奶牛的开始时间和结束时间
       只要总体的开始时间不大于结束时间那就一直找下去,在找的过程中不断的更新最后的结束时间,还需要存储一下不成立时
       结束的下标,因为已经对奶牛拍好了顺序,所以不要担心会漏掉成立的奶牛.
    代码:
      
    #include<iostream>
    #include<algorithm>
    using namespace std;
    struct zone{
        int begin;
        int end;
    }arr[25000];
    bool cmp(const zone &a,const zone &b)
    {
        if(a.begin==b.begin)    return a.end > b.end;
        return a.begin < b.begin;
    }
    int N,T;
    int solve()
    {
        int end=0;
        int ans=0;
        int index=0;
        int i,j;
        while(end<T)
        {
            int begin=end+1;
            for(i=index;i<N;i++)
            {
                //起点可以被覆盖 
                if(arr[i].begin<=begin)
                {
                    if(arr[i].end>=begin)
                    {
                        end=max(arr[i].end,end);
                    }
                }
                else
                {
                    index=i;
                    break;
                }
            }
            if(begin>end)
            {
                return -1;
            }
            else
            {
                ans++;
            }
        }
        return ans;
    }
    int main()
    {
        while(cin>>N>>T)
        {
            for(int i=0;i<N;i++)
            {
                cin>>arr[i].begin>>arr[i].end;
            }
            sort(arr,arr+N,cmp);
            int result=solve();
            cout<<result<<endl;
        }
        return 0;
    }
  • 相关阅读:
    进度三
    centos7 安装nodejs 最新版及yarn安装
    sed -i 增加或删减注释#
    解决go编译报错../github.com/golang/freetype/truetype/face.go:13:2: cannot find package "golang.org/x/image/font" in any of:
    解决go打包时报错 models/util/code.go:23:2: cannot find package "github.com/boombuler/barcode" in any of:
    Dockerfile文件说明
    docker exec 报错:error: code = 13 desc = invalid header field value "oci runtime error
    Docker中 /var/lib/docker/overlay2/磁盘爆满,如何解决?
    CentOS7默认安装的/home中转移空间到根目录/
    Docker存储驱动之--overlay2
  • 原文地址:https://www.cnblogs.com/acmblog/p/9572699.html
Copyright © 2020-2023  润新知