【POJ】3190 - Stall Reservations

http://poj.org/problem?id=3190

n头奶牛要在指定的时间内挤牛奶,一台机器只能对一个奶牛工作。给定每头奶牛的开始时间和结束时间,问最少需要多少机器,并输出每只牛所在的机器序号。

先按奶牛的开始时间从小到大排序,然后维护一个优先队列,以奶牛的结束时间小为优先。每次检查当前奶牛的开始时间是否大于堆顶牛奶的结束时间。若是,进入该机器挤奶。若否,增加一台新机器。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;

int n;
int now,ans;
struct Cow{
    int st,ed;
    int num;
    int now;
    bool operator< (const Cow &a)const{
        return ed>a.ed;
    }
    //这里使用  friend operator<(Cow a,Cow b){ ... }  报错
}cow[50005];

bool cmp1(Cow a,Cow b){
    return a.st<b.st;
}
bool cmp2(Cow a,Cow b){
    return a.num<b.num;
}
priority_queue <Cow> q;

int main(){

    cin >> n;
    for (int i=0;i<n;i++){
        int x,y;
        cow[i].num=i;
        scanf("%d%d",&x,&y);
        cow[i].st=x;
        cow[i].ed=y;
    }

    sort(cow,cow+n,cmp1);

    cow[0].now=1;
    q.push(cow[0]);
    ans=1;
    for (int i=1;i<n;i++){
        if (cow[i].st>q.top().ed){
            cow[i].now=q.top().now;
            q.pop();
            q.push(cow[i]);
        }
        else{
            ans++;
            cow[i].now=ans;
            q.push(cow[i]);
        }
    }

    printf("%d\n",ans);
    sort(cow,cow+n,cmp2);
    for (int i=0;i<n;i++){
        printf("%d\n",cow[i].now);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值