Recently Monocarp got a job. His working day lasts exactly m 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 d 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 d 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 ndistinct integers a1,a2,…,an (1≤ai≤m), where ai 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.
Input
4 5 3
3 5 1 2
Output
3
3 1 1 2
In the first example, Monocarp can take two coffee breaks during the first day (during minutes 1 and 5, 3 minutes will pass between these breaks). One break during the second day (at minute 2), and one break during the third day (at minute 3).
题意就是:n 共n次喝咖啡,m 最大时间点, d 两次喝咖啡之间的差值 问最少需要喝几天,才能都喝完。
直接暴力给每天赋值即可
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
typedef pair<int,int> p;//对 记录值和位置
set<p> s;//set中元素是对,则默认按第一个元素从小到大排,再按第二个元素从小到大排
set<p> ::iterator it;
int a[maxn],ans[maxn];
int main()
{
int n,m,d;
scanf("%d%d%d",&n,&m,&d);
d++;
for(int i=1;i<=n;i++){
scanf("%d",a+i);
s.insert(p(a[i],i));
}
int cnt=0;
while(!s.empty()){
int pos=s.begin()->second;//第一天都标记完了,标记下一天
ans[pos]=++cnt;
s.erase(s.begin());
while(1){//把这一天能标记的都删掉
it=s.lower_bound(p(a[pos]+d,0));//找到大于加d的第一个值位置
if(it==s.end()) break;
pos=it->second;
ans[pos]=cnt;//标记
s.erase(it);//删除
}
}
printf("%d\n",cnt);
for(int i=1;i<=n;i++){
printf("%d%c",ans[i],i==n?'\n':' ');
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
struct node{
int num,index,cnt;
node(int _num=0,int _index=0,int _cnt=0):num(_num),index(_index),cnt(_cnt){}
}a[maxn];
bool cmp(node a,node b){
return a.num<b.num;
}
int main(){
int n,m,d;
int b[maxn];
queue <node> q;
scanf("%d%d%d",&n,&m,&d);
int x;
for(int i=0;i<n;i++){
scanf("%d",&a[i].num);
a[i].index=i;
}
sort(a,a+n,cmp);
int cnt=1;
int i,j;
q.push(node(a[0].num,a[0].index,1));
for(i=1;i<n;i++){
if(a[i].num>q.front().num+d){
b[q.front().index]=q.front().cnt;
q.push(node(a[i].num,a[i].index,q.front().cnt));
q.pop();
}
else{
q.push(node(a[i].num,a[i].index,++cnt));
}
}
while(!q.empty()){
b[q.front().index]=q.front().cnt;
q.pop();
}
printf("%d\n",cnt);
for(i=0;i<n;i++){
printf("%d%c",b[i],i==n-1?'\n':' ');
}
return 0;
}