Codeforces Round #509 (Div. 2) - C - Coffee Break
题意:
有 n 个不同的休息时间,每天工作时间为m,任意两个休息点之间至少要隔d个时间,求最少工作天数,使得 n 个休息时间都休息了。
两天的休息点不会相互影响,因为题目中说到两天之间的时间默认 > d
对于每次设置休息点,就是记录一个上次休息时间 t +至少工作时间 d 为 pre,找到剩余的休息时间中最小的 > pre 的值,如果不存在则天数++,然后设置 pre = 0,然后继续找,直到所有的休息时间都被设置了。
查找的部分可以利用二分,但因为被设置过的时间要被移除,利用数组二分的话要更新数组(即数组下推),那样太慢了,考虑set的移除操作(大概logn),对于每个休息时间都只用一次二分查找和删除的操作,总的时间复杂度大概为(nlogn)。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#include <algorithm>
#include <functional>
#include <map>
#include <iomanip>
#include <vector>
#include <set>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int n, m, d, a[N];
set<int>b;
set<int>::iterator it;
int main()
{
map<int, int> mp;
scanf("%d%d%d", &n, &m, &d);
for(int i=1;i<=n;i++) {
scanf("%d", &a[i]); b.insert(a[i]);
}
int tot = 1, now = 1, ans = 0, sz = n;
while(sz) {
it = b.lower_bound(now);
if(it == b.end()) {now = 1; tot ++; continue;}
mp[*it] = tot; ans = tot;
now = *it + d + 1; if(now > m) now = 1, tot ++;
b.erase(*it); sz --;
}
printf("%d\n", ans);
for(int i=1;i<=n;i++) {
if(i-1) printf(" ");
printf("%d", mp[a[i]]);
}
printf("\n");
return 0;
}