Coffee Break(貪心+STL)

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.

題目大意:有一位員工想要利用喝咖啡來休息,他給了一個數組表示他想要喝咖啡的時間點(假設他喝咖啡用時1分鐘),老闆規定每次喝咖啡的時間間隔必須要大於d。問:他將給定數組的時間點都過一遍歷一遍最少(貪心所在)需要多長時間,並輸出每個時間點是在第幾天經歷的;

 

解題思路:貪心地選取喝咖啡的時間,我們儘量選取喝咖啡時間靠前的,然後貪心尋找能放在他後面最靠前的時間點,如果放不開就新開一天。

用map+queue

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include<algorithm>
#include<iomanip>
#define SIS std::ios::sync_with_stdio(false)
#define ll long long
#define INF 0x3f3f3f3f
#define mod 998244353
const int MAXN = 1e9 + 7;
const double PI = 3.14159265358979;
using namespace std;
const int N = 1e6 + 5;
/*int gcd(int a,int b)
{
	return b==0?a:gcd(b,a%b);
}
ll _power(int a, int b)//計算a^b;
{
	int ans=1, res=a;
	while(b){
		if(b&1) ans=ans*res%mod;
		res=res*res%mod;
		b>>=1;
	}
	return ans%mod;
	*/
queue<int> q;
map<int, int> ma;
int a[N], same[N];
int main() {
	int n, m, d;
	int sign = 1;
	SIS;
	cin >> n >> m >> d;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		same[i] = a[i];
	}
	
	sort(same+1, same + n+1);
	ma[same[1]] = 1;
	q.push(1);
	for (int i = 2; i <= n; i++) {
		int top = q.front();
		if (same[i] - same[top] > d) {
			ma[same[i]] = ma[same[top]];
			q.pop();
		}
		else {
			sign++;
			ma[same[i]] = sign;
		}
		q.push(i);
	}
	cout << sign << endl;
	for (int i = 1; i <= n; i++) {
		if (i == 1) cout << ma[a[i]];
		else
			cout << " " << ma[a[i]];
	}
	cout << endl;
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章