Cod sursa(job #125847)

Utilizator byndrsnAlina Ene byndrsn Data 20 ianuarie 2008 19:18:54
Problema Partitie Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int N, D, d;
vector<int> num, id, which, last;
ifstream fin;
ofstream fout;

bool cmp(const int& a, const int& b) {
	return (num[a] - num[b] < 0);
}

int go(int x) {
	int n = last.size();

	for (int i = n - 1; i >= 0; -- i)
		if (x >= last[i] + D)
			return i;

	return -1;
}


int main(void) {
	fin.open("partitie.in");
	fout.open("partitie.out");

	fin >> N >> D;

	for (int i = 0; i < N; ++ i) {
		fin >> d;
		num.push_back(d);
		id.push_back(i);
	}

	sort(id.begin(), id.end(), cmp);

	which.resize(N, -1);

	for (int i = 0; i < N; ++ i) {
		int pos = go(num[id[i]]);

		if (pos == -1) {
			last.push_back(num[id[i]]);
			which[id[i]] = last.size();
			continue;
		}

		which[id[i]] = pos + 1;
		last[pos] = num[id[i]];
	}

	fout << last.size() << endl;

	for (int i = 0; i < N; ++ i)
		fout << which[i] << endl;

	return 0;
}