博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 1041C(贪心+set)
阅读量:4677 次
发布时间:2019-06-09

本文共 3624 字,大约阅读时间需要 12 分钟。

题面:

C. Coffee Break

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

4 5 33 5 1 2

Output

Copy

33 1 1 2

Input

Copy

10 10 110 5 7 4 6 3 2 1 9 8

Output

Copy

22 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.

 

题意:

    把n分成x块,使得每块里的数两两相差大于d,求最小的x.

题目分析:

    因为要求使得x最小,因此此时的最优策略必定是从最小的时间开始取,一直向上拓展。

    因此我们可以贪心得每次都是由最小的时间开始取,然后不断的+d进行筛选即可。

    同时需要注意,因为d可能可以很小,而x的值可能很大,因此我们直接模拟去做可能会超时,因此我们可以考虑用二分去处理。我们可以用一个set去存储每一个值,每次做用lower_bound取查找第一个大于等于x+d+1的值即可,同时因为选过了的不能重复选,因此我们还需要将原来到结果删除即可。

代码:

#include 
#define maxn 200005using namespace std;typedef long long ll;struct Node{ int val,id; bool operator<(const Node &b)const{ if(val==b.val) return id
st;int ans[maxn];int main(){ int n,m,d; //freopen("in.txt","r",stdin); cin>>n>>m>>d; for(int i=1;i<=n;i++){ int num; scanf("%d",&num); st.insert(Node(num,i)); } ll tmp=0,day=1; while(!st.empty()){ if(st.lower_bound(Node(tmp,-1))==st.end()){ tmp=0; day++; } else{ auto it=*st.lower_bound(Node(tmp,-1)); ans[it.id]=day; tmp=it.val+d+1; st.erase(it); } } cout<
<

 

转载于:https://www.cnblogs.com/Chen-Jr/p/11007190.html

你可能感兴趣的文章
P5018-对称二叉树
查看>>
ASP .Net Core系统部署到SUSE Linux Enterprise Server 12 SP3 64 具体方案
查看>>
构建之法第二章个人技术和流程
查看>>
第三周linux学习
查看>>
[POJ1496 Word Index]
查看>>
jQuery插件AjaxFileUpload可以实现ajax文件上传
查看>>
20155321 2016-2017-2 《Java程序设计》第十周学习总结
查看>>
牢记!SQL Server数据库开发的二十一条注意点
查看>>
图片的Base64编码
查看>>
USACO Section 4.2 The Perfect Stall(二分图匹配)
查看>>
Linked List Cycle II
查看>>
java后台通过Servlet给用户发送手机短信验证码,第一次写勿喷,欢迎转载
查看>>
php多虚拟主机配置
查看>>
软件测试作业1
查看>>
JavaScript———从setTimeout与setInterval到AJAX异步
查看>>
Google Performance工具,你还不会用?Git走起。
查看>>
WScript.Shell对象的run和exec(脚本调用其他程序)
查看>>
Codeforces Round #364 (Div. 2),只有A与B
查看>>
Android版数据结构与算法(二):基于数组的实现ArrayList源码彻底分析
查看>>
Python 学习 --简单购物车程序
查看>>