Cod sursa(job #2504612)

Utilizator ardutgamerAndrei Bancila ardutgamer Data 5 decembrie 2019 11:55:53
Problema Partitie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <set>
#include <vector>
#include <functional>

using namespace std;

const int NMAX = 300005;

struct num {
	int val, pos;
	bool operator<(const num& other) const
	{
		return val < other.val;
	}
};

multiset<num>st;

int sol[NMAX];
num v[NMAX];
int n, d, cnt = 0;


int main()
{
	ifstream cin("partitie.in");
	ofstream cout("partitie.out");
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> d;
	for (int i = 1; i <= n; i++)
	{
		cin >> v[i].val;
		v[i].pos = i;
	}
	sort(v + 1, v + n + 1);
	for (int i = 1; i <= n; i++)
	{
		num temp = { v[i].val - d,1 };
		multiset<num>::iterator it = st.lower_bound(temp);
		if (it == st.end())
		{
			st.insert({ v[i].val,++cnt });
			sol[v[i].pos] = cnt;
			continue;
		}
		int pos = it->pos;
		sol[v[i].pos] = pos;
		st.erase(it);
		st.insert({ v[i].val,pos });
	}
	cout << cnt << "\n";
	for (int i = 1; i <= n; i++)
		cout << sol[i] << "\n";
	return 0;
}