Stall Reservations(3190)

大致题意:有一些奶牛只能在指定的时间内挤牛奶,而一个机器只能同时对一个奶牛工作,给你每头奶牛的指定时间的区间,问你最小需要多少机器,并且输出,每头奶牛具体在由哪个机器来挤奶。

做法:先将这些区间按开始时间从小到大排序, 然后用优先队列判断当前所有区间中结束时间最早的和现在这个区间开始时间的大小,如果当前区间的开始时间比较大,那这头奶牛就是由取出的机器来执行,不然就要加一台机器

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
struct s{
    int st, ed, idx, bel;
}pp[50100];
struct num{
    int no, val;
    num(){}
    num(int xx, int yy):no(xx), val(yy){}
    bool operator < (const num &a) const{
        return val > a.val;
    }
};
bool cmp(s a, s b){
    return a.st < b.st;
}
bool cmp2(s a, s b){
    return a.idx < b.idx;
}
priority_queue<num>que;
int n;
int ans[50100];
int main(){
    int i;
    while(scanf("%d" ,&n) != EOF){
        while(!que.empty()){
            que.pop();
        }
        int tot = 0;
        for(i = 1; i <= n; i++){
            scanf("%d%d", &pp[i].st, &pp[i].ed);
            pp[i].idx = i;
        }
        sort(pp+1, pp+1+n, cmp);
        int res = 1;
        que.push(num(1, pp[1].ed));
        pp[1].bel = 1;
        tot = 1;
        for(i = 2; i <= n; i++){
            num temp = que.top();
            if(pp[i].st > temp.val){
                que.pop();
                que.push(num(temp.no, pp[i].ed));
                pp[i].bel = temp.no;
            }
            else{
                res++;
                que.push(num(++tot, pp[i].ed));
                pp[i].bel = tot;
            }
        }
        printf("%d\n", res);
        sort(pp+1, pp+1+n, cmp2);
        for(i = 1; i <= n; i++)
            printf("%d\n", pp[i].bel);
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值