题干:

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 a1,a2,…,ana1,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 aiai, 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.

Input

The first line contains three integers nn, mm, dd (1≤n≤2⋅105,n≤m≤109,1≤d≤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 a1,a2,…,ana1,a2,…,an (1≤ai≤m)(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 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 aiai. Days are numbered from 11. 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 11 and 55, 33 minutes will pass between these breaks). One break during the second day (at minute 22), and one break during the third day (at minute 33).

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.

题目大意:

最近Monocarp找到了一份工作。他的工作时间正好是m分钟。在工作期间,Monocarp想要在特定的时刻喝咖啡:有n分钟a1,a2,…,an,当他能够并且愿意喝咖啡休息(为了简单起见,让我们考虑一下每个咖啡休息时间正好持续1分钟)。

然而,Monocarp的老板不喜欢Monocarp经常喝咖啡休息时间。所以对于给定的咖啡休息时间是在分钟ai上,Monocarp必须选择他在这一分钟内喝咖啡的日期,以便在任何两个咖啡休息时间之间每天至少有d分钟的时间。Monocarp还希望在最短的工作日内享受这n个咖啡休息时间(他不计算他不工作的天数,而且他在这样的日子里不喝咖啡)。考虑到任何工作日结束到下一个工作日开始之间超过d分钟的时间。

在给定的n分钟内决定一天的时间,在这一分钟内单粒种子应该喝咖啡休息。你必须尽量减少花费的天数。

解题报告:

这题的题意确实是比较那懂,大致就是有某人要在n天内喝n杯咖啡(因为m貌似没啥用,因为就算每天只喝一杯,最多就用n天喝完啊),喝每杯咖啡都花费1单位时间,如果两杯咖啡在一天喝,那么它们之间至少要相隔d min,要求输出最少几天可以喝完,并输出对应题目输入的每一杯咖啡是在第几天喝的。

答案可以很多种但是我们只需要构造最好想的,我们可以贪心找出其中一组解。最早喝的那杯咖啡在某一天中一定是第一杯,我们就直接让它在第一天被喝,由此可以推出下一杯咖啡时间至少要 >= ai + d + 1,于是一直往下找即可,如果找不到就说明这一天已经不能喝了,另开一天即可。

AC代码:

#include<bits/stdc++.h>
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));}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));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;
}

总结:

还是提醒一些坑:

1.用了STL就一定要注意是否为空!it迭代器是否有效!这题巧了,不需要在while判断if(空了) break;因为你lowerbound或者upperbound的时候如果没找到(包括了set中已经没元素的情况了),都直接就是ss.end()了,所以这些情况直接包含在这里break了。

2.这种语句套路也很常用啊,while(不空) 搞第一个;while(1) 然后 set中二分 , if(it == ss.end()) break;  、、 ;类似这个意思。

3.关于pair和lowerbound。这题这么写it = ss.lower_bound(make_pair(a[pos] + d + 1,0));也可以,但是

it = ss.upper_bound(make_pair(a[pos] + d,0));   就不行!!!这牵扯到set中对pair类型默认排序的问题。

你如果用upperbound  , pair的second就必须用INF,或者一个特别大的数。不然就会wa。

如果是int类型的set,就没事。

 set<int> s;set<int> :: iterator itt;s.insert(1);s.insert(2);s.insert(3);itt = s.upper_bound(2);cout << *itt;    //输出3

但是pair类型,对于这个题。

3 3 1

1 2 3

这个样例就过不了。输出的就全是1,但是显然不符合题意。

【CodeForces-1041C】Coffee Break(贪心,STL,set二分维护,题意难,有坑,SJ题,构造)(知识点总结)相关推荐

  1. Codeforces - 1118D2 - Coffee and Coursework (Hard Version) - 二分

    https://codeforces.com/problemset/problem/1118/D2 也是很好想的一个二分啦. 验证m的可行性的时候,肯定是把最多咖啡因的咖啡先尽可能平均分到每一天,因为 ...

  2. codeforces(D2. Coffee and Coursework (Hard Version))二分答案

    这题一看很容易想到二分,但我一开始想偏了,我是想枚举天数,然后二分去验证天数是否满足,但是这样二分验证天数是否满足这一步卡住了,写不出来. 然后我改成二分天数,验证天数是否满足这一步改成暴力计算,这样 ...

  3. 蒟蒻的第一篇博客CF1041C Coffee Break(二分+贪心+set)

    CF1041C Coffee Break(二分+贪心+set) 描述 Recently Monocarp got a job. His working day lasts exactly mm min ...

  4. Codeforces 1041C(贪心+set)

    传送门 题面: C. Coffee Break time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. CF1041C Coffee Break

    CF1041C Coffee Break 题目大意: 给定nn个数和一个kk,这nn个数都不超过mm 每次从没被去掉的数里面选一个数aa,去掉aa,然后可以任意一个b(b>a+k)b(b> ...

  6. CodeForces - 348A Mafia (贪心)

    题目大意:题目在这里 就是说有n个人在玩游戏,每个人都想玩足够的局数,给出每个人想要玩的局数,考虑到每局都需要一个人来做裁判,求出如果满足每个人的要求,至少需要玩几盘. 题目思路: 其实这道题是出在模 ...

  7. F 魏迟燕的自走棋(思维+贪心+并查集维护联通块/左部点配对边<=2的匈牙利)

    https://ac.nowcoder.com/acm/contest/9984/F 参考:F 魏迟燕的自走棋(贪心+并查集) 将每个人看成一个点,武器的能力值抽象成边,这样就转化成图论的模型了. 然 ...

  8. CodeForces - 967D Resource Distribution(贪心+二分+构造)

    题目链接:点击查看 题目大意:给出 n 个机器,每个机器可以处理 a[ i ] 的工作,现在有两个工作需要处理,工作量分别为 x1 和 x2,可以将一个工作分配给 k 个机器同时完成,需要满足: k ...

  9. HDU 5246 超级赛亚ACMer 【贪心】【STL版本二分】

    超级赛亚ACMer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

最新文章

  1. 清北学堂模拟赛d5t4 套路
  2. 第1次作业+105032014074
  3. Word 邮件合并中的域的格式的2个小定义
  4. 网络通信-2(TCP通信、ServerSocket、Socket)
  5. 【每日一题】7月13日题目精讲—Kingdom
  6. Thunder团队第三周 - Scrum会议6
  7. 云服务器怎么安装声音驱动_Windows服务器怎么装虚拟声卡驱动呢
  8. php二分法 冒泡 快速排序,PHP 常见算法【冒泡排序, 快速排序, 插入排序, 取舍排序, 二分法查找, .】...
  9. java 创建日程到期提醒_晓日程 微信日历加桌面日历,规划时间,掌握未来
  10. 操作系统知识--线程
  11. python体测成绩数据分析统计服_体测成绩数据分析
  12. Linux 平台中调试 C/C++ 内存泄漏方法
  13. server2012卸载oracle,Windows Server 2008 R2卸载干净ORACLE 11G
  14. 智慧工地解决方案施工升降机智能监控系统
  15. 适用于Creo 4.0-6.0的PTC Creo EMX 12.0.2.8
  16. 211西北大学,成立国家超级计算西北大学分中心!
  17. Rust中iter()和into_iter()的区别
  18. 漏斗模型_绘制漏斗图
  19. windows10 该值受安全引导策略保护,无法进行修改或删除。禁用驱动程序强制签名
  20. 中英文说明书丨艾美捷双链RNA定量试剂盒

热门文章

  1. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第50篇]什么是BLS基于Weil对的签名方案?
  2. Java学习笔记11-1——Spring5
  3. 小白学python需要多久_小白学Python | 你还在说你入不了门吗
  4. vb.net怎么调用fastreport报表_财务分析-企业财务管理报表模板制作实现智能化的财务运营...
  5. python spark dataframe_pyspark dataframe 常用操作
  6. 一建机电实务教材电子版_20年一建其实并不难,官方出版:复习题集(精修),速做速提90分...
  7. jquery交换数组元素位置_跟我一起学jQuery——第一集
  8. epoll或者kqueue的原理是什么?
  9. C/C++中“空语句”的说明
  10. python requests https_解决python的requests模块访问私有SSL证书产生的报错问题