随笔-141  评论-9  文章-3  trackbacks-0

/*
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;
}
posted on 2010-11-21 00:45 小阮 阅读(114) 评论(0)  编辑 收藏 引用 所属分类: USACO

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理