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

news/2024/7/2 23:48:32

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;
}

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


http://lihuaxi.xjx100.cn/news/286090.html

相关文章

GRU情感分类问题再战

import tensorflow as tf import numpy as np import tensorflow as keras from tensorflow.keras import losses,Sequential,optimizers,layers,datasetsbatchsz128#批量大小 total_words10000#词汇表大小N_vocab max_review_len80#句子最大长度 s&#xff0c;大于的句子部分…

图论 ---- CF1209F. Koala and Notebook(多位数字拆边+BFS)

题目链接 题目大意&#xff1a; 给你一个nnn个点mmm条边的无向联通图&#xff0c;每条边上面都有一条权值就是输入时候的位置&#xff0c;然后问你从111号点出发到其他n−1n-1n−1个点最小权值是多少&#xff1f;路径权值是路径上面的数字拼接起来的结果&#xff0c;不是相加 …

32岁程序员,失业4个月45次面试经历,与君共勉

程序员求职面试&#xff08;微信号&#xff1a;CoderJob&#xff09;整理内容综合自&#xff1a;网络一个32岁的程序员&#xff0c;失业4个月的45次面试&#xff0c;终于入职了&#xff0c;与君共勉。看到这么多面试经历&#xff0c;网友们也激动了。有网友说&#xff1a;面试确…

公开课 | 用AI给旧时光上色!详解GAN在黑白照片上色中的应用

在改革开放40周年之际&#xff0c;百度联合新华社推出了一个刷屏级的H5应用——用AI技术为黑白老照片上色&#xff0c;浓浓的怀旧风勾起了心底快被遗忘的时光。想了解如何给老照片上色&#xff1f;本次公开课中&#xff0c;我们邀请到了百度高级研发工程师李超&#xff0c;他的…

iOS开发中的 地区转经纬 经纬度转地区

2019独角兽企业重金招聘Python工程师标准>>> 参考 iOS 根据地名获取经纬度 iOS 根据经纬度显示地名 - (void)setCity {[[LoginUserInfo sharedLoginUserInfo] latitude];NSLog("%",[[LoginUserInfo sharedLoginUserInfo] latitude]);NSLog("------%…

求一个数的因子个数/因子和/质因子 C/C++实现

求一个数的因子个数时间复杂度O√n ll get_number(ll x){ll num0;for(ll i1;i*i<x;i){if(x%i0) num2; if(i*ix) num1;}return num; }求一个数的因子和时间复杂度O√n ll get_number(ll x){ll num0;for(ll i1;i*i<x;i){if(x%i0) numix/i;if(i*ix) numi;}return num…

poj1422(最小路径覆盖问题)

最小路径覆盖数: 对于一个DAG&#xff08;有向无环图&#xff09;&#xff0c;选取最少条路径&#xff0c;使得每个 顶点属于且仅属于一条路径。路径长度可以为零&#xff1b;&#xff08;有向图中找一些路径&#xff0c;使之覆盖了图中的所有顶点&#xff0c;就是任意一个顶点…

主席树 ---- CF 1422F. Boring Queries(由离线推出在线如何求的 ,求解多次询问的区间LCM)

题目链接 题目大意&#xff1a; 给你nnn个数&#xff0c; 每次往第iii个数里面里面乘aaa&#xff0c;问你这nnn个数的LCM\text{LCM}LCM是多少&#xff1f; 解题思路&#xff1a; 多个数的lcm不是所有数的乘积除以所有数的gcd&#xff0c;如 4 8 3 正确求法是每个数分解质因数…