A - Coffee Break(优先队列+map)

比较偏思维的贪心题,非常像导弹拦截系统。解释细节放在代码注释中。

赛后补的,代码不知道那个天才想出来的。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <sstream>
#include <queue>
#include <stack>
using namespace std;
int main()
{
    cin.tie(0)->sync_with_stdio(false);
    int n, m, d, cnt = 1;
    cin >> n >> m >> d;
    priority_queue<int, vector<int>, greater<int>> q;// 小根堆维护时间表的最小下标,也就是最早时间(因为排序后下标越靠前时间越早)
    vector<int> a(n + 1), b(n + 1);
    map<int, int> mp;//时间表
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        b[i] = a[i];//保整原序列顺序
    }
    sort(b.begin() + 1, b.end());//排序,贪心实现
    mp[b[1]] = 1;
    q.push(1);
    for (int i = 2; i <= n; i++)
    {
        int t = q.top();
        if (b[i] - b[t] > d)
        {
            mp[b[i]] = mp[b[t]];//更新时间表中时间安排最早的一个
            q.pop();
        }
        else//如果最小的也无法使该时间安排进去,就给时间表新开一天
        {
            cnt++;
            mp[b[i]] = cnt;
        }
        q.push(i);//扩展优先队列,保证每个点只进入一次
    }
    cout << cnt << endl;
    for (int i = 1; i <= n; i++)
    {
        cout << mp[a[i]] << " ";
    }
}

/*******************************

Problem A. Coffee Break

样例

10 10 1
10 5 7 4 6 3 2 1 9 8

输出
2
2 1 1 2 2 1 2 1 1 2

                      by布克波波
*******************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值