CF1041C Coffee Break(二分+贪心+set)

描述

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.

输入

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.

输出

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.

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 

题解:我们选取时刻的时候,贪心思想尽量每次把最小的安排进一天中,如果找不到的话就在开启新的一天并重复以上操作。

对于频繁的查找和删除某一个数,那肯定是用set了;
这样的话时间复杂度为O(nlog(n))了;

代码

#pragma GCC optimize(3,"Ofast","inline")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <math.h>
#include <string>
#include <list>
#include <set>
#include <algorithm>
#define MaxN  0x3f3f3f3f3f
#define MinN  0xc0c0c0c0c0
using namespace std;
struct wazxy{int i,num;friend bool operator < (const wazxy & a,const wazxy & b){   //当我们set容器放入结构体时,需要重载一下运算符return a.num<b.num;    //自定义排序规则}
};
set<wazxy> a;
wazxy x;
int position[200010];   //建立一个数组储存他是第几次操作;
int main()
{int n,m,k;cin>>n>>m>>k;for(int i=1;i<=n;i++){scanf("%d",&x.num);x.i=i;a.insert(x);   //将内容放进set容器;}int xx=(*a.begin()).num,cnt=1;  //贪心  找最小的那个;position[(*a.begin()).i]=cnt;a.erase(a.begin());     //已经将数纪录下来防止重复使用所以删掉;for(int i=2;i<=n;i++){set<wazxy>::iterator it;x.num=xx+k+1;it=a.lower_bound(x);    //若搜不到则返回.end;if(it!=a.end()){         //如果搜到了;position[(*it).i]=cnt;   //记录当前操作次数xx=(*it).num;      a.erase(it); continue;  }cnt++;        //如果搜不到操作次数+1,进入下一个操作的开始it=a.begin();   position[(*it).i]=cnt;xx=(*it).num;a.erase(it);}printf("%d\n",cnt);   for(int i=1;i<=n;i++)  printf("%d ",position[i]);return 0;
}

第一篇博客 终
本人小白一枚,忘各位神犇勿喷。

蒟蒻的第一篇博客CF1041C Coffee Break(二分+贪心+set)相关推荐

  1. 蒟蒻的第一篇博客——博弈

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.博弈论基本要素 二.博弈论类型 四大博弈 总结 前言 太无聊了搞个博弈专题vanvan~ 博弈论(Game The ...

  2. 【杂记】纪念第一篇博客

    第一篇博客 在老师的要求下,本蒟蒻第一次开了个博客(其实还有个洛谷博客来着--但是老师让我在CSDN开),纪念一下.高一一年学OI没学到多少,相比其他强校的大犇可以说是非常弱了(%%%). 但是自己选 ...

  3. 这是我的第一篇博客!

    这是我的第一篇博客~ 这个博客账号将会由一个技术菜鸡来总结一些我学习的东西,也许会总结的乱七八糟,也许会错误频频,甚至看不懂在写个什么(emmm有点过分)...但是无所谓啦,目的只是希望以后能向各路大 ...

  4. 点滴篇(一) 第一篇 博客

    我的第一篇博客~~~ 这个博客就为技术贴吧,以后我会分享自己在学习中的点点滴滴

  5. 第一篇博客,用以规划我的编程人生

    本人编程小白.第一次写技术博客,希望制定一个学习计划,让自己慢慢成长为一名优秀的程序猿. 学习方向是成为一名全栈工程师. 前端方面:会用bootstrap设计网页,会用jQuery完成动作. 后端方面 ...

  6. Python中的TCP的客户端UDP学习----第一篇博客

    Python中的TCP的客户端&UDP学习--第一篇博客 PS: 每日的怼人句子"我真想把我的脑子放到你的身体里,让你感受一下智慧的光芒" 先说UDP流程 发送: 创建套接 ...

  7. “Hello, my first blog”------第一篇博客的仪式感

    本人在校大学生一枚,开通博客,主要是想记录自己的学习过程,分享自己的学习经历.记得大一的时候,很多不懂的操作和知识,都是在博客上找到了相应的解决办法.但比较讽刺的是,很多时候,曾经解决了的问题,当再次 ...

  8. 第一篇博客《import tensorflow的问题解决》

    第一篇博客<import tensorflow的问题解决> 参考文章: (1)第一篇博客<import tensorflow的问题解决> (2)https://www.cnbl ...

  9. 第一篇博客——自我介绍篇

    首先介绍下自己.我来自山东理工大学,专业为矿业工程,是一名非科班专业应届毕业生. 这是我的第一篇博客.之前从来没接触过博客,一直都是在笔记本记录,沾边一点的也就是发发朋友圈.今天是我写博客的第一天,从 ...

最新文章

  1. 阿里云弹性计算-图形工作站(公测)发布
  2. 【FPGA】FIFO的Verilog设计之同步FIFO的设计
  3. 小程序与服务器通讯,微信小程序之即时通讯WebSocket
  4. iccar conference oral presentation
  5. python如何使用ppip安装xlwt_Python-xlwt库的基本使用
  6. linux mtime参数,linux find mtime参数详解
  7. c语言的数据有常量与,C语言数据与常量.ppt
  8. 改完计算机名自动重启 vbs,ghost后自动修改IP和计算机名的VBS脚本
  9. SimpleDateFormat多线程问题
  10. c语言写识别电压的程序,PIC单片机C语言编程实例——交流电压测量
  11. 华为手机的视频剪辑功能居然这么强大,太实用啦
  12. 使用vba操作工作表,实现报表汇总
  13. 项目管理-1-忆往昔
  14. 【Web】Hexo+Butterfly+Github+Coding搭建个人博客
  15. 【渗透实战】web渗透实战,相对高安全级别下,详细分析整个渗透过程以及介绍社工的巧妙性,拿一站得数十站,(漏洞已交)
  16. 人工智能技术概述与入门
  17. OWA附件隐藏excle格式下载按钮
  18. 熬夜总结!最全的Pycharm常用快捷键大全!
  19. 【博学谷学习记录】超强总结,用心分享|AjaxHTTP(二)
  20. 江苏理工学院计算机考研,江苏理工学院考研成绩创新高 学风建设见成效

热门文章

  1. 文档信息的向量化-NNLM模型和word2vec
  2. Linux之软件卸载 apt-get
  3. CPU 周期信号、节拍周期信号、节拍脉冲信号三者之间的关系是什么?
  4. MATLAB_图形学_形态学课程II
  5. Windows系统下制作一个记事本以语音方式读出你输入的文字 以及放到开机启动项,开机自启动读出语音!
  6. OpenCV图像处理常用手段
  7. ​专为初学者设计——最小的神经网络
  8. 如何通俗的理解面向对象编程
  9. JQuery中的queue()及dequeue()
  10. Linux命令(30):tar命令-归档工具