Cod sursa(job #656837)

Utilizator iuyuIoana Orsa iuyu Data 5 ianuarie 2012 13:40:31
Problema Sortare Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 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; 
}