So far I have no idea what the permutation group is ... What is the right thing to say? jpg
Following from hzwer:http://hzwer.com/3905.html
#include <iostream>#include <cstdio>#include <algorithm>using namespaceStdConst intn=10005;intN,w[n],a[n],v[n];structqwe{intX,id;} A[M];BOOLcmpConstQwe &a,ConstQwe &b) {returna.x<b.x;}intRead () {intR=0, f=1;CharP=getchar (); while(p>' 9 '|| p<' 0 ') {if(p=='-') f=-1; P=getchar (); } while(p>=' 0 '&&p<=' 9 ') {r=r*Ten+p-48; P=getchar (); }returnR*f;}intMain () {n=read (); for(intI=1; i<=n;i++) W[i]=b[i].x=read (), b[i].id=i; Sort (b+1B+1+N,CMP); for(intI=1; i<=n;i++) a[b[i].id]=i;intans=0; for(intI=1; i<=n;i++)if(!v[i]) {ints=1, P=i,mn=w[i],sum=w[i]; v[i]=1; while(!v[a[p]]) p=a[p],v[p]=1, Mn=min (Mn,w[p]), sum+=w[p],s++; Ans+=min (sum+mn* (s-2), mn+sum+b[1].x* (S+1)); } printf ("%d\n", ans);return 0;}
Bzoj 1697: [Usaco2007 feb]cow sorting cattle sort "permutation group"