Cod sursa(job #253384)

Utilizator 630r63Ilinca George Mihai 630r63 Data 5 februarie 2009 18:50:05
Problema Sortare Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.98 kb
#include <stdio.h>  
 #include <string.h>  
   
 #define FIN "sortare.in"  
 #define FOUT "sortare.out"  
   
 #define NMAX 5001  
 #define NMIN 4  
   
 int HMAX[NMAX];  
 int pozitie[NMAX][NMIN];  
 int SIR_PERM[NMAX];  
 int N,NR;  
 int poz,i;  
 int S[NMAX];  
   
   
   
 void sortare()  
 {  
 int aux,i1,i2;  
   
 for (i1=1;i1<=2;++i1)  
      for (i2=i1+1;i2<=3;++i2)  
       if (pozitie[i][i1]>pozitie[i][i2])  
           {  
            aux=pozitie[i][i1];  
            pozitie[i][i1]=pozitie[i][i2];  
            pozitie[i][i2]=aux;  
           }  
 }  
   
   
   
 void read_data()  
 {  
   
 freopen(FIN,"rt",stdin);  
 freopen(FOUT,"wt",stdout);  
   
 scanf("%d", &N);  
   
 for (i=2;i<=N;++i)  
      scanf("%d %d %d", &pozitie[i][1] /*A*/, &pozitie[i][2] /*B*/, &pozitie[i][3] /*C*/);  
   
 }  
   
   
   
 void solve()  
 {  
 int j,l;  
   
 read_data();  
   
 pozitie[1][1]=3;  
 pozitie[1][2]=3;  
 pozitie[1][3]=3;  
 HMAX[0]=0;  
 HMAX[1]=1;  
   
 for (i=2;i<=N;++i)  
      {  
       sortare();  
       // cazul 1 cand A=B si B=C  
       if (pozitie[i][1]==pozitie[i][2] && pozitie[i][2]==pozitie[i][3])  
       HMAX[i]=HMAX[i-1]+1;  
       // cazul 2 cand A<>B si B=C  
       if (pozitie[i][1]<pozitie[i][2] && pozitie[i][2]==pozitie[i][3])  
       HMAX[i]=HMAX[i-1]+1;  
       // cazul 3 cand A=B si B<>C  
       if (pozitie[i][1]==pozitie[i][2] && pozitie[i][2]<pozitie[i][3])  
       HMAX[i]=HMAX[i-1]+1;  
       // cazul 4 cand A<>B si B<>C  
       if (pozitie[i][1]<pozitie[i][2] && pozitie[i][2]<pozitie[i][3])  
       HMAX[i]=HMAX[i-2]+1;  
        }  
   
 S[1]=N;  
 NR=1;  
 while (S[NR]>1)  
      {  
       i=S[NR];  
       //cazul 1  
       if (pozitie[i][1]<pozitie[i][2] && pozitie[i][2]<pozitie[i][3])  
      {  
      NR++;  
      S[NR]=i-2;  
      }  
      else  
      {  
      NR++;  
      S[NR]=i-1;  
      }  
 }  
   
 //construiesc sirul pe cele doua cazuri generale  
   
 for (l=NR;l>=1;--l)  
      {  
       poz=S[l];  
       if (poz==1)  
       SIR_PERM[1]=1;  
       else  
          {  
           // cazul 1 general  
           if (pozitie[poz][1]<pozitie[poz][2] && pozitie[poz][2]<pozitie[poz][3])  
           {  
            for (i=poz-1;i>=pozitie[poz][2]+1;--i)  
             SIR_PERM[i]=SIR_PERM[i-1];  
             SIR_PERM[pozitie[poz][2]]=poz-1;  
            for (i=poz;i>=pozitie[poz][3]+1;--i)  
             SIR_PERM[i]=SIR_PERM[i-1];  
             SIR_PERM[pozitie[poz][3]]=poz;  
            }  
           else  
             //cazul 2 general  
             {  
              // cazul 1 secundar  
              if (pozitie[poz][1]==pozitie[poz][2] && pozitie[poz][2]==pozitie[poz][3])  
              {  
               for (i=poz;i>=pozitie[poz][1]+1;--i)  
                    SIR_PERM[i]=SIR_PERM[i-1];  
                    SIR_PERM[pozitie[poz][1]]=poz;  
              }  
              // cazul 2 secundar  
              else  
                if (pozitie[poz][1]==pozitie[poz][2] && pozitie[poz][2]<pozitie[poz][3])  
                    {  
                 for (i=poz;i>=pozitie[poz][2]+1;--i)  
                      SIR_PERM[i]=SIR_PERM[i-1];  
                      SIR_PERM[pozitie[poz][2]]=poz;  
                    }  
                    //cazul 3 secundar  
                    else  
                  if (pozitie[poz][1]<pozitie[poz][2] && pozitie[poz][2]==pozitie[poz][3])  
                      {  
                       for (i=poz;i>=pozitie[poz][2]+1;--i)  
                        SIR_PERM[i]=SIR_PERM[i-1];  
                        SIR_PERM[pozitie[poz][2]]=poz;  
                      }  
                 }  
                }  
            }  
 }  
   
   
 void write_data()  
 {  
   
 printf("%d\n", HMAX[N]);  
   
 for (i=1;i<=N;++i)  
 printf("%d ", SIR_PERM[i]);  
   
 }  
   
   
   
 int main()  
 {  
 solve();  
 write_data();  
 return 0;  
}