C. Coffee Break CodeForces - 1041C (贪心)

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 n distinct 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.

Examples
Input
4 5 3
3 5 1 2
Output
3
3 1 1 2
Input
10 10 1
10 5 7 4 6 3 2 1 9 8
Output
2
2 1 1 2 2 1 2 1 1 2
Note
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).

In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.

题意:英语不好的我,用了翻译软件翻译出来也是让我一脸懵逼,相信很多人百度是为了看懂题意
题意大概是这样:
有个工作时长为m 的强迫症很喜欢喝咖啡,他写了个计划,计划在n个时间点各喝一次咖啡,因为他是强迫症,所以必须恰好在那个时间点喝咖啡,他才会很舒服,可是他的老板看他很不爽,于是规定他两次和咖啡的时间必须超过d,现在要你帮助他,算出他最少几天才能每个时间都喝过一次咖啡,并且输出第i杯咖啡,第几天喝。

思路:很明显,这就是一个贪心题,将一天的时间价值最大化,也就是让他一天可以喝咖啡的杯数尽可能多,那要怎么选择时间点呢,肯定是时间最早的最好,所以每次选时间最早的设为st,然后再去剩下的时间找有没有大于等于(st+d+1),如果有必然可以同一天喝,所以以此类推,最后全部喝完就得到了答案

代码:

#include<bits/stdc++.h>
#define LL long long
#define Max 100005
#define Mod 1e9+7
const LL mod=1e9+7;
const LL inf=0x3f3f3f3f;
using namespace std;
LL n,m,d,ans[2*Max];
set<LL>s;
map<LL,int>mi;
int main()
{
    scanf("%lld%lld%lld",&n,&m,&d);
    LL x;
    for(int i=0;i<n;i++){
        scanf("%lld",&x);
        mi[x]=i;//记录原始位置,因为等下要输出
        s.insert(x);//set默认小到大排序,而且可以二分查找
    }
    int day=1;
    LL t=*s.begin();//开始为最小的时间点
    ans[mi[t]]=day;
    s.erase(s.begin());
    while(s.size()){
       set<LL>::iterator next=s.lower_bound(t+d+1);//找有没有<=t+d+1的
        if(next!=s.end()){//如果存在那么直接可以同一天完成
            ans[mi[*next]]=day;//记录在哪一天完成
            t=*next;
            s.erase(next);
        }else{//不存在,就开始新的一天
            day++;
            t=*s.begin();//新的一天时间点继续从最小的开始
            s.erase(s.begin());
            ans[mi[t]]=day;
        }
    }
    printf("%d\n",day);
    for(int i=0;i<n;i++)
        printf("%d ",ans[i]);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值