Recently Monocarp got a job. His working day lasts exactly mm minutes. During work, Monocarp wants to drink coffee at certain moments: there are n minutes a1,a2,…,an, when he is able and willing to take a coffee break (for the sake of simplicity let’s consider that each coffee break lasts exactly one minute).
However, Monocarp’s boss doesn’t like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute ai, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least dd minutes pass between any two coffee breaks. Monocarp also wants to take these n coffee breaks in a minimum possible number of working days (he doesn’t count days when he is not at work, and he doesn’t take coffee breaks on such days). Take into account that more than dd minutes pass between the end of any working day and the start of the following working day.
For each of the n given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent.
Input
The first line contains three integers n, m, d(1≤n≤2⋅105,n≤m≤109,1≤d≤m)— the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks.
The second line contains nn distinct integers a1,a2,…,an (1≤ai≤m), where aiai is some minute when Monocarp wants to have a coffee break.
Output
In the first line, write the minimum number of days required to make a coffee break in each of the n given minutes.
In the second line, print n space separated integers. The i-th of integers should be the index of the day during which Monocarp should have a coffee break at minute ai. Days are numbered from 1. If there are multiple optimal solutions, you may print any of them.
4 5 3
3 5 1 2
3
3 1 1 2
10 10 1
10 5 7 4 6 3 2 1 9 8
2
2 1 1 2 2 1 2 1 1 2
题目大意:
最近Monocarp找到了一份工作。他的工作时间正好是m分钟。在工作期间,Monocarp想要在特定的时刻喝咖啡:有n分钟a1,a2,…,an,当他能够并且愿意喝咖啡休息(为了简单起见,让我们考虑一下每个咖啡休息时间正好持续1分钟)。
然而,Monocarp的老板不喜欢Monocarp经常喝咖啡休息时间。所以对于给定的咖啡休息时间是在分钟ai上,Monocarp必须选择他在这一分钟内喝咖啡的日期,以便在任何两个咖啡休息时间之间每天至少有d分钟的时间。Monocarp还希望在最短的工作日内享受这n个咖啡休息时间(他不计算他不工作的天数,而且他在这样的日子里不喝咖啡)。考虑到任何工作日结束到下一个工作日开始之间超过d分钟的时间。
在给定的n分钟内决定一天的时间,在这i分钟内Monocarp应该喝咖啡休息。你必须尽量减少花费的天数。
题目转自:https://blog.csdn.net/qq_41289920/article/details/82962569
#include
using namespace std;
const int MAX = 2e5 + 10;
const int INF = 0x3f3f3f3f;
int a[MAX],ans[MAX];
set<pair<int,int> > ss;
set<pair<int,int> > :: iterator it;
int n,m,d,cnt;
int main()
{
cin>>n>>m>>d;
for(int i = 1; i<=n; i++) {
scanf("%d",a+i);
ss.insert(make_pair(a[i],i)); ///make_pair成对输进容器中
//应该是按第一个元素进行排序
}
while(!ss.empty()) {
int pos = ss.begin() -> second;
ans[pos] = ++cnt;
ss.erase(ss.begin());
while(1) {
it = ss.upper_bound(make_pair(a[pos]+d,INF));///upper_bound在set中也有
///如果upper_bound没有找到,返回end()值(越界值)
///只要比a[pos]+d大,就定位到那个下标。inf确保防止特殊情况
if(it == ss.end()) break;
pos = it->second;
ans[pos] = cnt;
ss.erase(it);
}
}
printf("%d\n",cnt);
for(int i = 1; i<=n; i++) printf("%d%c",ans[i],i==n ? '\n' : ' ');
return 0;
}