Description
Give you a sequence that increments him, the cost of exchanging two elements is two numbers and minimizing the cost.
Sol
Permutation group + discretization.
To restore a sequence to an ascending order, the number of positions he and he is going to need to be exchanged, thus forming a permutation.
For a permutation of a tangential circle, we can prove that the minimum cost of this is the smallest element in the circle (the size of the forward circle-1) + The other number and the minimum element.
1 forward loops of size n, at least n-1 times to become N 1 yuan rings
When \ (size=1,2\), it is obviously established.
When \ (size>2\), each exchange of 1 elements will split the circle into two disjoint permutations, which must be torn down n-1 times.
And then there's the problem is that we can pull a minimum number to exchange, so we need the price, with the smallest and the smallest in the circle, and then the smallest * (with a circle size-1)
So this problem is obvious, as for not continuous, we can be discretized.
Code
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream>using namespace std;const int N = 1005;int n,cnt,ans,ca=1;int r[n],a[n],b[n],c[n],v[n],s1;int p[n][n],s[n],h[n];inline int in (int x=0, Char Ch=getchar ()) {while (ch> ' 9 ' | | ch< ' 0 ') Ch=getchar (); while (ch>= ' 0 ' &&ch<= ' 9 ') x= (x<<3) + (x<<1) +ch-' 0 ', Ch=getchar (); return x; }int Main () {while (~SCANF ("%d", &n)} {for (int i=1;i<=n;i++) r[i]=in (); cnt=ans=0;s1=r[1];for (int i=1;i<=n;i+ +) if (R[I]<S1) s1=r[i];for (int i=1;i<=n;i++) a[i]=r[i];sort (a+1,a+n+1); for (int i=1;i<=n;i++) b[a[i]]=i;for ( int i=1;i<=n;i++) c[i]=b[r[i]];//for (int i=1;i<=n;i++) printf ("%d%c", a[i], "\ n" [i==n]);//for (int i=1;i<=n;i + +) printf ("%d%c", r[i], "\ n" [I==n]),//for (int i=1;i<=n;i++) printf ("%d%c", b[i], "\ n" [i==n]);//for (int i=1;i<=n ; i++) printf ("%d%c", c[i], "\ n" [i==n]); memset (V,0,sizeof (v)); for (int i=1,t;i<=n;i++) if (!v[i]) {t=i,++cnt,p[cnt][ 0]=0,s[cnt]=r[i],h[cnt]=0;while (!v[t])V[t]=1,p[cnt][++p[cnt][0]]=r[t],s[cnt]=min (S[cnt],r[t]), h[cnt]+=r[t],t=c[t];} for (int i=1;i<=cnt;i++) for (int j=0;j<=p[i][0];j++) printf ("%d%c", p[i][j], "\ n" [j==p[i][0]]); for (int i=1;i <=cnt;i++) {ans+=min (s[i]* (p[i][0]-1) +h[i]-s[i],s[i]*2+s1*2+s1* (p[i][0]-1) +h[i]-s[i]);} if (!ans) break;printf ("Case%d:%d\n", Ca++,ans);} return 0;}
Codevs 2845 the price of sorting