比较偏思维的贪心题,非常像导弹拦截系统。解释细节放在代码注释中。
赛后补的,代码不知道那个天才想出来的。
#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布克波波
*******************************/