Cod sursa(job #163678)

Utilizator sims_glAlexandru Simion sims_gl Data 22 martie 2008 14:54:45
Problema Sortare Scor 100
Compilator cpp Status done
Runda preONI 2008, Runda Finala, Clasa a 10-a Marime 1 kb
#include <stdio.h>

#define nm 5010

int n, last[nm], crt[nm], next[nm], sol[nm];

int main()
{
	freopen("sortare.in", "r", stdin);
	freopen("sortare.out", "w", stdout);

	scanf("%d", &n);

	crt[0] = crt[1] = 1;
	sol[0] = sol[1] = 1;

	for (int i = 1; i < n; ++i) {
		int x, y, z;

		scanf("%d%d%d", &x, &y, &z);

		if (x > y)
			x += y, y = x - y, x -= y;
		if (y > z)
			y += z, z = y - z, y -= z;
		if (x > y)
			x += y, y = x - y, x -= y;

		if (x == y || y == z) {
			if (x == y)
				next[x] = 1;
			else
				next[z] = 1;

			for (int tmp = 1, j = 1; j <= i + 1; ++j)
				if (!next[j])
					next[j] = crt[tmp++] + 1;

			sol[i + 1] = sol[i] + 1;
		} else {
			next[x] = 1;
			next[y] = 2;

			for (int tmp = 1, j = 1; j <= i + 1; ++j)
				if (!next[j])
					next[j] = last[tmp++] + 2;

			sol[i + 1] = sol[i - 1] + 1;
		}

		for (int j = 1; j <= i + 1; ++j)
			last[j] = crt[j], crt[j] = next[j], next[j] = 0;
	}

	printf("%d\n", sol[n]);

	for (int i = 1; i <= n; ++i)
		printf("%d ", crt[i]);
	printf("\n");

	return 0;
}