poj3190

对牛按照milk的开始时间排序。
对优先队列里牛的milk的结束时间排序,结束时间最早的作为que.top()和牛i做对比,如果牛i的开始时间大于(因为文中说include A和B,所以不能取等号。)que.top()的结束时间,那么将该牛的牛栏(stall)设为与que.top()相同的牛栏,同时que.pop();否则新建一个牛栏,将牛i的牛栏设为该新建的牛栏。

//1492k, 219ms
#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

const int MAX_N = 50005;

struct Interval {
    int from;
    int to;
    int pos;

    //优先队列里结束时间最早的优先
    bool operator < (const Interval &a) const {
        if(to == a.to) return from > a.from;//如果结束时间相同,则开始时间早的优先 
        else           return to > a.to;
    } 

}interval[MAX_N];

priority_queue<Interval> que;

int n, stall[MAX_N];

//对牛的milk时间进行排序,开始时间早的放前面。 
bool cmp(Interval a, Interval b) {
    if(a.from == b.from) return a.to < b.to;
    else                 return a.from < b.from;
} 

int main() {

    freopen("in.txt", "r", stdin);

    while(~scanf("%d", &n)) {
        for(int i=0; i<n; i++) {
            scanf("%d%d", &interval[i].from, &interval[i].to);
            interval[i].pos = i;
        }

        sort(interval, interval+n, cmp);

        int ans=1;
        stall[interval[0].pos] = 1;
        que.push(interval[0]);
        for(int i=1; i<n; i++) {
            if(!que.empty() && que.top().to<interval[i].from) {
                stall[interval[i].pos] = stall[que.top().pos];
                que.pop();
            } else {
                ans++;
                stall[interval[i].pos] = ans;
            }
            que.push(interval[i]);
        }

        printf("%d\n", ans);
        for(int i=0; i<n; i++)
            printf("%d\n", stall[i]);

        while(!que.empty()) que.pop();
    }

    fclose(stdin);

    return 0;
}

转载于:https://www.cnblogs.com/StevenL/p/6818282.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值