**
Coffee Break题解
**
题目
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 aj, a2, … , dn, 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;, 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
Input
The first line contains three integersn,m,d(1<n<2-10’,n<m<10%,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 n distinct integers aı,Q3,…,@n (1 <a; < m), where a; 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 ofthen
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 a,. Days are numbered from 1. Ifthere
are multiple optimal solutions, you may print any of them.
题意
一个人每天有m分钟的时间喝咖啡,告诉你他每次和咖啡需要的时间(n个),同时喝咖啡的间隔为d ,
问你如何合理的将这n次喝咖啡时间分配到尽可能少的天数里。
思路
时间间隔为d,又有n个时间,可以想到这是个等差数列,所以从小的时间开始枚举每个数列直到时间+d超过m,每开始一个数列的枚举,就计一天。
可以开pair的set来存数列,第一维枚举数列,第二维枚举天数。
代码
#include<bits/stdc++.h>
const int N=2e5+10;
#define Pii pair<int,int>
set<Pii>t;
int a[N];
void solve()
{
cin>>n>>m>>d;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
t.insert({x,i});
}
int cnt=0;
while(!t.empty())
{
int now=t.begin()->second,nowv=t.begin()->first;
a[now]=++cnt;
t.erase(t.begin());
while(1)
{
auto tt=t.lower_bound({nowv+d+1,0});
if(tt==t.end())
break;
now=tt->second;
nowv=tt->first;
a[now]=cnt;
t.erase(tt);
}
}
cout<<cnt<<endl;
for(int i=1;i<=n;i++)
cout<<a[i]<<" ";
}
int main()
{
solve();
}