POJ 2376Cleaning 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. 

等等。。。

题目链接:POJ 2376Cleaning Shifts


本题是给定一个n表示有n个数据吧区间1~T填满,要求使用最小

解法:贪心大法,对于所给的数据进行排序,按照开始时间从小到大排序

用一个ftime来记录结束时间,最后走动啊time>T。

用一个flag来记录每一个小区间的最大值。

不要忘记如果最开始不是1 就是输出-1不要犹豫


以下是我的代码

#include <vector>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
struct sa{
int x,y;}a[25010];
int cmp(const sa a,const sa b){
    if(a.x==b.x)
    return a.y<b.y;
    return a.x<b.x;
}
int main(){
    int n,t;
    cin>>n>>t;
    for(int i=0;i<n;i++)
    scanf("%d%d",&a[i].x,&a[i].y);
    sort(a,a+n,cmp);
    if(a[0].x!=1){
        cout<<-1<<endl;
        return 0;
    }//直接输出-1
    int time=0;
    int k=0;
    for(int i=0;i<n;i++){
        if(a[i].x!=1){
            time=a[i-1].y;
            k=i;
            break;
        }
    }//用k来找1的最后一个
//    cout<<k<<" :"<<time<<endl;
    if(k==0){
        cout<<1<<endl;
        return 0;
    }
//	第一个就到最后
    if(a[k-1].y>=t){
        cout<<1<<endl;
        return 0;
    }//1的区间就到尾了
    int flag=0;
    int sum=1;
    for(int i=k;i<n;i++){
        if(a[i].x<=time+1){
            flag=max(flag,a[i].y);
        }
        if(a[i+1].x>time+1){
            time=max(time,flag);
            flag=0;
            sum++;
//            i--;
        }
        if(i==n-1){
            time=max(time,flag);
            sum++;
        }
        if(time>=t){
            break;
        }
    }
//    cout<<time;
    if(time<t)
    cout<<-1<<endl;
    else cout<<sum<<endl;
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值