标签:name margin res ast exactly cond data- class distinct
给定nn个数和一个kk,这nn个数都不超过mm
每次从没被去掉的数里面选一个数aa,去掉aa,然后可以任意一个b(b>a+k)b(b>a+k),然后去掉任意一个c(c>b+k)c(c>b+k),以此类推
问最少能选多少个aa,然后输出每个数都是选第几个aa的时候被去掉的
一行三个整数n,m,k
再一行nn个整数,表示给定的数
第一行一个整数,表示最少选aa的个数
第二行nn个整数,表示每个数都是选第几个aa时被去掉的
Recently Monocarp got a job. His working day lasts exactly mm minutes. During work, Monocarp wants to drink coffee at certain moments: there are nn minutes a_1, a_2, \dots, a_na1?,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 a_iai? , 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 nn 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 nn 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.
The first line contains three integers nn , mm , dd (1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)(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 a_1, a_2, \dots, a_na1?,a2?,…,an? (1 \le a_i \le m)(1≤ai?≤m) , where a_iai? is some minute when Monocarp wants to have a coffee break.
输出格式:
In the first line, write the minimum number of days required to make a coffee break in each of the nn given minutes.
In the second line, print nn space separated integers. The ii -th of integers should be the index of the day during which Monocarp should have a coffee break at minute a_iai? . Days are numbered from 11 . If there are multiple optimal solutions, you may print any of them.
10 10 1 10 5 7 4 6 3 2 1 9 8
2 2 1 1 2 2 1 2 1 1 2
/* 很显然是一个贪心,从左到右查找第一个时间点,然后以此为起点,向后尽量多的删去其他时间点 当知道a时间点时,我们要求的是满足b>a+k的最小的b,于是可以用二分查找找到b的位置,然后做标记 */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int b[200010]; int n,m,d,k,num,pre,Pre,Num; struct node{ int val,id,tag,bel; bool operator < (const node w)const{ return val<w.val; } node(){tag=0;} }a[200010]; int Search(int x){ int l=x,r=n,mid,ans=n+1; while(l<=r){ mid=(l+r)>>1; if(a[mid].val>a[x].val+d)ans=mid,r=mid-1; else l=mid+1; } for(int i=ans;i<=n;i++) if(!a[i].tag)return i; return n+1; } int main(){ scanf("%d%d%d",&n,&m,&d); for(int i=1;i<=n;i++) scanf("%d",&a[i].val),a[i].id=i; Pre=1; sort(a+1,a+n+1); for(int i=1;i<=n;i++)b[a[i].id]=i; while(k!=n){ num++; for(int i=Pre;i<=n;i++) if(!a[i].tag){ pre=i;Pre=pre+1; break; } a[pre].tag=1;k++;a[pre].bel=num; while(1){ Num=Search(pre); if(Num>n)break; a[Num].bel=num; a[Num].tag=1; k++;pre=Num; } } printf("%d\n",num); for(int i=1;i<=n;i++) printf("%d ",a[b[i]].bel); return 0; }
标签:name margin res ast exactly cond data- class distinct
原文地址:https://www.cnblogs.com/thmyl/p/11173352.html