zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    jmeter测试mysql数据库之JDBC请求
    接口测试浅谈
    python import xxx 与 from xxx import xx 模块引入的区别
    交互模式下测试python代码及变量的四则运算
    python入门之一python安装及程序运行
    zabbix命令之:zabbix_get命令
    snmpwalk工具使用
    zabbix基础知识
    Centos7.5 rpm安装zabbix_agent4.0.3
    mysql常用命令
  • 原文地址:https://www.cnblogs.com/acmblog/p/9572699.html
Copyright © 2011-2022 走看看