Cod sursa(job #163654)

Utilizator mariusdrgdragus marius mariusdrg Data 22 martie 2008 14:52:09
Problema Sortare Scor 0
Compilator cpp Status done
Runda preONI 2008, Runda Finala, Clasa a 10-a Marime 2.21 kb
#include<stdio.h>
#include<algorithm>

using namespace std;
//mai faci greedy

const int maxn = 5003;
const int maxst = 4;
int P[maxst][maxn];
int ad[maxn];
int le;
int N;
int A[maxn][maxst];

int ada(int &x)
{
        ++x;
        x %= 3;
}

void fac(int x,int x1,int poz)
{
        P[x][poz] = 1;
        int j = 1;
        for(int i = 1;i <= le; ++i)
        {
                if (P[x][i] == 0)
                {
                        P[x][i] = P[x1][j] + 1;
                        ++j;
                }
        }
}

int main()
{
        freopen("sortare.in","r",stdin);
        freopen("sortare.out","w",stdout);
        scanf("%d",&N);
 //       printf("%d\n",sizeof(P) + sizeof(A) + sizeof(ad));
        for(int i = 2;i <= N; ++i)
        {
                scanf("%d %d %d",&A[i][1],&A[i][2],&A[i][3]);
                sort(A[i] + 1,A[i] + 4);
        }
        P[0][1] = 1;
        P[1][1] = 1;P[1][2] = 2;
        ad[1] = 1;
        ad[0] = 1;
        int x =  2;
        int x1 = 1;
        int x2 = 0;
        for(le = 3;le <= N; ++le)
        {
                memset(P[x],0,sizeof(P[x]));
                if (A[le][1] == A[le][2])
                {
                        fac(x,x1,A[le][1]);
                        ad[x] = ad[x1] + 1;
                }
                if (A[le][2] == A[le][3] && A[le][1] != A[le][2])
                {
                        fac(x,x1,A[le][2]);
                        ad[x] = ad[x1] + 1;
                }
                if (A[le][1] != A[le][2] && A[le][1] != A[le][3])
                {
                        ad[x] = ad[x2] + 1;
                        P[x][A[le][1]] = 1;
                        P[x][A[le][2]] = 2;
                        int j = 1;
                        for(int i = 1;i <= le; ++i)
                        {
                                if (P[x][i] == 0)
                                {
                                        P[x][i] = P[x2][j] + 2;
                                        ++j;
                                }
                        }
                }
                ada(x);ada(x1);ada(x2);
        }
        printf("%d\n",ad[x1]);
        for(int i = 1;i <= N; ++i)
                printf("%d ",P[x1][i]);
        printf("\n");
        return 0;
}