Cod sursa(job #1661423)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 23 martie 2016 20:58:19
Problema Sortare Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;

ifstream fin("sortare.in");
ofstream fout("sortare.out");

struct Quick {

	int a, b, c;
	Quick() {}
	Quick(int a, int b, int c) {

		if (a > b)
			swap(a, b);
		if (a > c)
			swap(a, c);

		if (b > c)
			swap(b, c);

		this->a = a;
		this->b = b;
		this->c = c;

	}

} operations[5005];

int n;
int sol[5005];

inline void setValue(int val, int pos) {

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

		if (sol[i] != 0)
			continue;

		--pos;
		if (pos != 0)
			continue;

		sol[i] = val;
		return;

	}

}

int main() {

	fin >> n;

	operations[1] = Quick(1, 1, 1);
	for (int i = 2; i <= n; ++i) {

		int a, b, c;
		fin >> a >> b >> c;

		operations[i] = Quick(a, b, c);

	}

	int maxSteps = 0;
	for (int i = n; i > 0; --i) {

		++maxSteps;

		if (operations[i].a != operations[i].b && operations[i].b != operations[i].c) {

			setValue(i - 1, operations[i].a);
			setValue(i, operations[i].b - 1);
			--i;

		}
		else if (operations[i].b == operations[i].c) {

			setValue(i, operations[i].b);

		}
		else {

			setValue(i, operations[i].a);

		}

	}

	fout << maxSteps << '\n';
	for (int i = 1; i <= n; ++i)
		fout << sol[i] << ' ';
	fout << '\n';

	return 0;

}

//Trust me, I'm the Doctor!