【题解】poj3190 贪心+堆优化

题目链接
按照开始吃草的时间把牛排序。用一个小根堆维护每个畜栏最后一头牛结束吃草的时间,尝试把当前的牛安排在堆顶

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define _rep(i,a,b) for(int i=(a);i<=(b);i++)
const int N=5e4+10;
int n,idx[N],pos;
struct node{
    int st,ed,id;
    bool operator<(const node&rhs)const{
    return ed>rhs.ed||(ed==rhs.ed&&st>rhs.st);}
}cow[N];
inline bool cmp(const node&a,const node&b)
{
    return a.st<b.st||(a.st==b.st&&a.ed<b.ed);
}
priority_queue<node>que;
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    _rep(i,1,n)scanf("%d%d",&cow[i].st,&cow[i].ed),cow[i].id=i;
    sort(cow+1,cow+n+1,cmp);
    idx[cow[1].id]=1;
    pos=1;
    que.push(cow[1]);
    _rep(i,2,n)
    {
        node tmp=que.top();
        if(cow[i].st>tmp.ed)idx[cow[i].id]=idx[tmp.id],que.pop();
        else idx[cow[i].id]=++pos;
        que.push(cow[i]);
    }
    printf("%d\n",pos);
    _rep(i,1,n)printf("%d\n",idx[i]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值