POJ-2376-Cleaning Shifts

原题
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.
题意:
有n头奶牛,每一头奶牛都有自己的工作时间,先将一天分成t个工作时段,问能否用最少的牛完成工作,某个时段可以有两头牛一起工作,若是不能完成输出-1,能完成输出所需最少牛数。
题解:
这道题目的贪心思想:
每一次都选起始时间再上一次结束时间之前的奶牛,并且找结束时间最大的那一头奶牛,即可,若是中间有空缺,或者开始和结尾不能完成工作输出-1;
更多细节见代码:
附上AC代码:

#include <iostream>
#include <algorithm>
using namespace std;
int n,t,vis;
struct node
{
    int st,ed;
}a[25005];
bool cmp(node a,node b)
{
    if(a.st==b.st)
        return a.ed>b.ed;//若是起始时间相同,则按照结束时间从大到小排序
    return a.st<b.st;//按照开始时间从小到大排序
}
int main()
{
    ios::sync_with_stdio(false);//减少cin消耗的时间
    while(cin>>n>>t)
    {
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].st>>a[i].ed;
    }
    sort(a+1,a+n+1,cmp);//排序
    if(a[1].st>1)//如果最小的开始时间都大于1,则注定不能完成任务
    {
        cout<<"-1"<<endl;
        continue;
    }
    int start=a[1].ed,cnt=1,maxi;//由于当st相同时按照ed从大到小排序,所以第一个的结束时间一定最长,所以一定会用第一个
    for(int i=1;i<=n+1;i++)
    {
        if(a[i].st<=start+1&&a[i].ed>start)
        {
            maxi=i;//记录ed最大的那一头奶牛的下标
            while(a[i].st<=start+1&&i<=n)//在满足条件的情况下,找出结束时间最长的那一头
            {
                if(a[i].ed>a[maxi].ed)
                {
                    maxi=i;
                }
                i++;
            }
            start=a[maxi].ed;//更新开始时间
            i=maxi;//下一次的i从maxi+1开始
            cnt++;//所需牛数加一
            if(start>=t)//如果结束时间已经覆盖了一整天可以直接推出循环,合理剪枝
                break;
        }
    }
    if(start<t)cout<<-1<<endl;//如果循环结束最后任然不能完成任务
    else cout <<cnt<< endl;
    }
    return 0;
}

欢迎评论!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值