标签:main 文章 poj logs col namespace struct csdn tor
置换群还是没有深刻的理解,但是做了这题确实让我理解好多
参考文章:
使数组有序的最小交换次数
http://blog.csdn.net/gettogetto/article/details/69389810
邝斌带我飞~~
http://www.cnblogs.com/kuangbin/archive/2012/09/03/2669013.html
#include <stdio.h> #include <string.h> #include <algorithm> #define LL long long using namespace std; #define maxn 10010 struct nod{ int x,i; bool operator<(const nod&a)const{return x<a.x;} }; nod a[maxn]; int n; int main(){ scanf("%d",&n); int mi=maxn*maxn; for(int i=1;i<=n;i++ )scanf("%d",&a[i].x),a[i].i=i,mi=min(mi,a[i].x); sort(a+1,a+n+1); int ans=0; // for(int i=1;i<=n;i++)printf("%d %d\n",a[i].x,a[i].i); for(int i=1;i<=n;i++){ if(!a[i].i)continue; int low=a[i].x,it=a[i].i; int beg=it,s=a[i].x,n=1; while(a[it].i!=beg){ // printf("%d\n",a[it].x); s+=a[it].x;n++;low=min(low,a[it].x); int tmp=it; it=a[it].i;a[tmp].i=0; } // printf("beg :%d s :%d low:%d n:%d \n",beg,s,low,n); if(n!=0){ ans+=min((n-2)*low+s,(n+1)*mi+s+low); } } printf("%d\n",ans); return 0; }
标签:main 文章 poj logs col namespace struct csdn tor
原文地址:http://www.cnblogs.com/MeowMeowMeow/p/7662720.html