2845 The price of sorting
time Limit: 1 sspace Limit: 32000 KBtitle Level: Golden Gold solvingTitle Description
Description
There is a column number that you want to sort (ascending). Sorting can only be done by swapping. Each exchange, you can select any two of the number of columns, exchange their positions, and the cost of the interchange is two Numbers. The total cost of sorting is the sum of all the exchange costs in the sorting process. The first requirement is to calculate the minimum cost for any given sequence to be ranked in ascending order.
Enter a description
Input Description
The input data consists of two Lines. The first row is a number n, which means that the number of n is a total, and the second row n a different integer (all positive integers less than 1000), indicating the number of columns
The input may contain multiple sets of test data (less than 50 groups) and the corresponding output is required for each input data
Output description
Output Description
For each input data, the output is the least Expensive. Format is case t:min
where T is the number of the data, starting from 1, min is the minimum cost of the data
Sample input
Sample Input
3
3 2 1
4
8 1 2 4
Sample output
Sample Output
Case 1:4
Case 2:17
Data range and Tips
Data Size & Hint
n<=1000
Category labels
Tags Click here to expandOn permutation group theorythe
following:
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 was apparently established.
When size>2, each exchange of 1 elements will split the circle into two disjoint permutations, which have to 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.
AC code:
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intn=1e3+Ten;intn,cnt,ans,cas;intr[n],a[n],b[n],c[n],s1;intp[n][n],s[n],h[n];BOOLvis[n];intmain () { while(SCANF ("%d", &n) = =1) {cnt=ans=0; Memset (vis,0,sizeofvis); Memset (p,0,sizeofp); Memset (h,0,sizeofh); for(intI=1; I<=n;i++) scanf ("%d", r+i); for(intI=1; I<=n;i++) a[i]=r[i]; Sort (a+1, a+n+1); s1=a[1]; for(intI=1; I<=n;i++) b[a[i]]=i; for(intI=1; I<=n;i++) c[i]=b[r[i]]; for(intI=1, T;i<=n;i++)if(!vis[i]) {s[++cnt]=r[t=i]; while(!vis[t]) vis[t]=1, p[cnt][++p[cnt][0]]=r[t],s[cnt]=min (s[cnt],r[t]), h[cnt]+=r[t],t=c[t]; } for(intI=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",++cas,ans); } return 0;}
2845 the price of sorting