Cod sursa(job #1492196)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 27 septembrie 2015 12:07:52
Problema Partitie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <deque>

#define DIM 300005
#define infile "partitie.in"
#define outfile "partitie.out"

using namespace std;

ifstream fin(infile);
ofstream fout(outfile);

struct Item {

	int index;

	int value;

};

struct PartitionManager {

	int value;

	int assignedPartition;

	PartitionManager(int value, int assignedPartition) {

		this->value = value;
		this->assignedPartition = assignedPartition;

	}

};


Item v[DIM];

deque<PartitionManager> deq;

int solution[DIM];


inline bool comp(const Item &a, const Item &b) {

	return a.value < b.value;

}


int main() {

	int n, d;

	fin >> n >> d;

	for (int i = 1; i <= n; ++i) {

		fin >> v[i].value;

		v[i].index = i;

	}


	sort(v + 1, v + n + 1, comp);


	solution[v[1].index] = 1;

	deq.push_back(PartitionManager(v[1].value, 1));

	int partitionCount = 1;

	for (int i = 2; i <= n; ++i) {

		if (v[i].value - deq.front().value >= d) {

			solution[v[i].index] = deq.front().assignedPartition;

			deq.push_back(PartitionManager(v[i].value, solution[v[i].index]));

			deq.pop_front();

		}
		else {

			++partitionCount;

			solution[v[i].index] = partitionCount;

			deq.push_back(PartitionManager(v[i].value, solution[v[i].index]));

		}

	}


	fout << partitionCount << '\n';

	for (int i = 1; i <= n; ++i) {

		fout << solution[i] << '\n';

	}

	return 0;

}

//Trust me, I'm the Doctor!