USACO 2.1.3 Sorting A Three-Valued Sequence

USACO 2.1.3 Sorting A Three-Valued Sequence

/**/ /*
ID: lorelei3
TASK: sort3
LANG: C++
*/


#include 
< fstream >
#include 
< iostream >
#include 
< algorithm >

using   namespace  std;

const   int  MAX  =   1005 ;

int  first[MAX], second[MAX];
int  n;

int  main() {

    
int i,j, npairs=0, ntriples=0, nswap=0;

    ifstream 
in("sort3.in");
    ofstream 
out("sort3.out");

    
in>>n;
    
for(i=0; i<n; ++i){
        
in>>first[i];
        second[i]
=first[i];
    }


    sort(second, second
+n);

    
for(i=0; i<n; ++i)
        
for(j=0; j<n; ++j)
            
if(first[i]!=second[i] && first[j]!=second[j]
                
&& first[i]==second[j] && second[i]==first[j]){
                first[i]  
= second[i];
                first[j] 
= second[j];
                npairs
++;
            }


    
for(i=0; i<n; ++i)
        
if(first[i]!=second[i])
            ntriples
++;
    
    nswap 
= npairs + ntriples/3 *2;

    
out<<nswap<<endl;
    
return 0;
}

你可能感兴趣的:(USACO 2.1.3 Sorting A Three-Valued Sequence)