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

题干:

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,但是显然不符合题意。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441537.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

盐务街属于一环吗_郑州步入“五环”时代,最早的“一环”原来是这里

近日&#xff0c;郑州“五环路”建设最新进展受到广泛关注。当郑州步入“五环”时代时&#xff0c;你是否想过郑州的最早的“一环”在哪里?来跟随记者的讲解&#xff0c;来一场时空逆行穿梭之旅&#xff0c;从历史深处寻找郑州最早的“一环”。△郑州五环示意图◆郑州现代“一…

C++ 输入输出加速挂(ACM竞赛常用)

好多种版本&#xff0c;今天给出其中之一&#xff0c;也是比较快的一种。 int read() {char ch getchar();int num 0;bool fl 0;for(; !isdigit(ch); ch getchar())if (ch-) fl 1;for(; isdigit(ch); ch getchar())num (num<<1)(num<<3)ch-48;if(fl) num …

oracle中那个日期怎么相减_oracle数据库中日期加减函数

1.oracle中当前系统时间加减用法select add_months(sysdate, 12) "NEW YEAR" from dual; /**系统时间12个月后的时间**/select extract(month from sysdate) "This Month" from dual; /**获取系统时间所在月份**/select (sysdate 1) "NEW DATE"…

【CodeForces - 1041D】Glider (枚举起点,双指针 或 二分终点,思维)(知识点总结)

题干&#xff1a; A plane is flying at a constant height of hh meters above the ground surface. Lets consider that it is flying from the point (−109,h)(−109,h) to the point (109,h)(109,h) parallel with OxOx axis. A glider is inside the plane, ready to s…

duilib vs2015 安装_FFmpeg视频播放器开发-FFmpeg简介与项目环境搭建(一)

前言&#xff1a;视频开发库有很多&#xff0c;例如微软的DirectShow&#xff1b;开源库OpenCV&#xff0c;当然OpenCV主要是图像处理&#xff0c;视频部分还是用的ffmpeg, 而且无法解码音频&#xff1b;SDL&#xff1b;大华和海康都有自己的库等等。音视频属于流媒体领域&…

【洛谷 - U43391】不是0-1背包的暴力AC(思维,二分,可转化为二元组问题,复习暴力dfs总结)

题干&#xff1a; https://www.luogu.org/problemnew/show/U43391 自01背包问世之后&#xff0c;小A对此深感兴趣。一天&#xff0c;小A去远游&#xff0c;却发现他的背包不同于01背包。 小A的背包最多能装W的价值 现有n件物品&#xff0c;分别为v1,v2,v3……vn 问如何存放物…

windowsthinpc虚拟内存_windows thin pc如何开启windows功能

没有“\DS\”目录 (子目录)没有“\Packages\”目录(子目录)没有“\FeaturePack\”目录(子目录)没有“\LangPacks\"目录(子目录)未找到“winemb-premiumcodecs-dolby-ac3-audioencoder.cab”未找到“WinEmb-PremiumCodecs-MPEG2andDolbyDecoder.cab”未找到“WinEmb-Premiu…

【牛客 - 185F】 假的数学游戏(斯特林公式,大数Java打表)

题干&#xff1a; 输入描述: 第一行&#xff1a;一个整数X 输出描述: 第一行&#xff1a;一个整数N 示例1 输入 复制 7 输出 复制 10 备注: 每个测试点所对应的X满足&#xff1a;第i个测试点输入的值为第i-1个测试点输入的值乘以10再加上7。特别的&#xff0c;第一个…

js怎么调用wasm_对于WebAssembly编译出来的.wasm文件js如何调用

WebAssembly也叫浏览器字节码技术 这里就不过多的解释了网上很多介绍主要是让大家知道在js里面如何调用执行它&#xff0c;我之前看WebAssemblyAPI时候反正是看得一脸懵逼也是为了大家能更快的入手这个比较新的技术吧这边写的一个dom是官方推荐的c/c编译的c代码1 int add (int …

*【HDU - 6333】Problem B. Harvest of Apples (莫队,逆元,组合数学)(这样预处理正确吗?)

题干&#xff1a; There are nn apples on a tree, numbered from 11 to nn. Count the number of ways to pick at most mm apples. Input The first line of the input contains an integer TT (1≤T≤105)(1≤T≤105) denoting the number of test cases. Each test c…

ruoyi 多模块部署_大数据时代,独立部署的商城系统具有哪些优势?

独立部署是把一个可部署软件包安装到一个指定IT环境上并让其按预定流程提供服务的过程。现如今&#xff0c;越来越多的商家开始搭建网上商城系统&#xff0c;从而为自己积攒多年的客户群体提供更为便捷的购物模式&#xff0c;让购物变得更加智能化。独立部署的商城系统具有哪些…

【SPOJ - DQUERY】D-query(权值树状数组 或 主席树 或 莫队)

题干&#xff1a; Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai1, ..., aj. Inpu…

freerdp 解压安装_Ubuntu下编译安装运作FreeRdp连接Windows

Ubuntu下编译安装运行FreeRdp连接Windows.linux下编译源码进行USB重定向到远程桌面的测试&#xff0c;经过参数的正确配置&#xff0c;可以正常的重定向。具体的操作步骤如下:⑴在虚拟机上安装git,安装命令如&#xff1a;sudo apt-get install git。⑵从GitHub下载FreeRdp源码&…

【HDU - 3328】Flipper (栈模拟)

题干&#xff1a; Problem Description Little Bobby Roberts (son of Big Bob, of Problem G) plays this solitaire memory game called Flipper. He starts with n cards, numbered 1 through n, and lays them out in a row with the cards in order left-to-right. (C…

trunk口不通防火墙_交换机S5700与防火墙USG5500无法对接Eth-trunk LACP-static模式

问题&#xff1a;交换机S5700与防火墙USG5500无法对接Eth-trunk LACP-static模式&#xff0c;两端正常配置后&#xff0c;端口状态显示错误&#xff0c;Eth-trunk端口无法up 。问题描述&#xff1a;交换机侧 GE0/0/5和GE 0/0/6 组成Eth-trunk3 通过LACP-static与防火墙对接&…

【HDU - 1850】Being a Good Boy in Spring Festival (尼姆博弈,nim,异或前缀和)

题干&#xff1a; 一年在外 父母时刻牵挂 春节回家 你能做几天好孩子吗 寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场 悄悄给爸爸买个小礼物 主动地 强烈地 要求洗一次碗 某一天早起 给爸妈用心地做回早餐 如果愿意 你还可以和爸妈说 咱们玩个小游戏吧 ACM课上学的呢…

comsol临时文件夹中有不支持的字符_Editor 常见问题

Editor的数据存在哪里&#xff1f;Editor所处理的数据&#xff0c;就是你自己本地电脑的文件夹、图片、文本文档。除了配置信息、一些必要的临时缓存之外&#xff0c;Editor没有特殊格式的数据。所以&#xff0c;数据都在电脑里&#xff1b;比如说需要备份的话&#xff0c;只要…

【51nod - 1098】 最小方差(基础数学,公式化简,前缀和,积的前缀和)

题干&#xff1a; 若x1,x2,x3……xn的平均数为k。 则方差s^2 1/n * [(x1-k)^2(x2-k)^2…….(xn-k)^2] 。 方差即偏离平方的均值&#xff0c;称为标准差或均方差&#xff0c;方差描述波动程度。 给出M个数&#xff0c;从中找出N个数&#xff0c;使这N个数方差最小。 Input …

【牛客 - 181B】送分题(另类求解a+b,二分)

题干&#xff1a; 对于一套题来说&#xff0c;没有一道送分题&#xff0c;就很不符合常理&#xff0c;但是我又懒得写送分题&#xff0c;所以你可以直接复制以下代码&#xff0c;即可ac本题. #include<cstdio>#include<iostream> using namespace std; int a,…

牧马人机械鼠标g3_性价比好的有线鼠标都有哪些?2020年12款热选游戏鼠标推荐...

决定一款鼠标好坏因素众多&#xff0c;我们常常发现一款鼠标&#xff0c;有些用户觉得非常好&#xff0c;也有些用户觉得不好&#xff0c;是因为大多数用户对于鼠标的尺寸大小、轻重、实际的手感等方面都会有所不同&#xff0c;这都很正常&#xff0c;所以适合自己才是最好的。…