Gym - 101911A A. Coffee Break

题目链接:http://codeforces.com/gym/101911/problem/A

A. Coffee Break
time limit per test2.0 s
memory limit per test256 MB
inputstandard input
outputstandard 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
inputCopy
4 5 3
3 5 1 2
outputCopy
3
3 1 1 2
inputCopy
10 10 1
10 5 7 4 6 3 2 1 9 8
outputCopy
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 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.


分析:

可以直接利用set里的.lower_bound()函数,如果用数组存的话,还得定义一个标记数组标记,比较麻烦

代码:
#include <bits/stdc++.h>

using namespace std;
#define ll long long 
const int N=2e6+10;

int a[N+1];
int b[N+1];
int num[N+1];
int pv[N+1];


map<int,int> ma;
set<int> s;

// int bin(int l, int r, int tmp)
// {
	
// }

int main()
{
	int n, m, d;
	
	scanf("%d%d%d",&n,&m,&d);
	d=d+1;
	s.clear();
	for(int i=0;i<n;i++)
	{
		scanf("%d",&b[i]);
		s.insert(b[i]);
	}

	int pos=0;
	int len=1;
	
	set<int>::iterator it;
	while(!s.empty())
	{
		it=s.begin();
		ma[*it]=len;
		s.erase(*it);
		it=s.lower_bound(*it+d);
		while(it!=s.end())
		{
			
			ma[*it]=len;
			s.erase(*it);
			it=s.lower_bound(*it+d);
		}
		if(it==s.end())
			len++;
		
		
	}
	// cout<<len<<endl;
	printf("%d\n",len-1);
	for(int i=0;i<n;i++)
	{
		if(i==0)
		{
			printf("%d",ma[b[i]]);
		}
		else
			printf(" %d",ma[b[i]]);
	}
	printf("\n");
	
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值