A 畜栏预定

题解:贪心

首先按照吃草时间将牛排序,建立一个集合,如果当前牛开始吃草时间不大于该畜栏最后一头牛结束吃草时间,则新建一个畜栏;(当前牛应该安排在当前集合中畜栏最后一头牛结束时间最早判断);

stl:优先队列:https://blog.csdn.net/weixin_52341477/article/details/119252627

上面题解证明:反证法:设存在一种方案,是的畜栏数目更少记得:m;

在上面题解做法中,当建立畜栏数目为m+1时,正在处理第i头牛;

所有牛都是按照开始吃草时间从小到大排序,所以在前m个畜栏数目最后一头牛开始时间一定小于等于第i头牛开始吃草时间

所以现在前 m个畜栏当中最小的结束时间大于等于第i个牛开始时间,所以至少需要m+1畜栏。矛盾;

# include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
const int N=1e6+10;
pair<PII,int>a[N];
int c[N];
int n;
priority_queue<PII,vector<PII>,greater<PII> >heap;
bool cmp(pair<PII,int>x,pair<PII,int>y) {
    return x.first.first<y.first.first;
}
int main() {
    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>a[i].first.first>>a[i].first.second;
        a[i].second=i;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1; i<=n; i++) {
        if(heap.empty()||a[i].first.first<=heap.top().first) {
            PII stu;
            stu.first=a[i].first.second;
            stu.second=heap.size();
            c[a[i].second]=stu.second;
            heap.push(stu);

        } else {
            PII stu=heap.top();
            heap.pop();
            stu.first=a[i].first.second;
            c[a[i].second]=stu.second;
            heap.push(stu);
        }
    }
    cout<<heap.size()<<endl;
    for(int i=1; i<=n; i++) {
        cout<<c[i]+1<<endl;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值