Cow sorting
| Time Limit: 2000MS |
|
Memory Limit: 65536K |
| Total Submissions: 6664 |
|
Accepted: 2602 |
Description
Farmer John ' sN (1≤  N ≤10,000) cows is lined up to being milked in the evening. Each cow have a unique "grumpiness" level in the range 1...100,000. Since Grumpy cows is more likely to damage FJ ' s milking equipment, FJ would like to reorder the cows in line so they is Lined up in increasing order of grumpiness. During This process, the places of any and cows (not necessarily adjacent) can be interchanged. Since Grumpy cows is harder to move, it takes FJ a total of  X + y units of time to exchange, cows whose grumpiness levels Are  X and   Y .
FJ calculate the minimal time required to reorder the cows.
Input
Line 1: A single integer:
N. Lines 2..
N+1:each line contains a single integer:line
I+1 describes the grumpiness of cow
I.
Output
Line 1: A single line with the minimal time required to reorder the cows in increasing order of grumpiness.
Sample Input
3231
Sample Output
7
Hint
2 3 1:initial order.2 1 3:after interchanging cows with grumpiness 3 and 1 (time=1+3=4). 1 2 3:after interchanging cows with grumpiness 1 and 2 (time=2+1=3). The puzzle : to transform a sequence of numbers into an ascending series, the weights of the exchange are two elements;We can get a number of permutation subgroups by this number of n numbers. for a given sequence { 3 1 7 10 6 1 3 6 7 10 } The sequence above becomes the following sequence. We can get 2 permutation subgroups (1, 3) (6, 7)。 Next there are two ways to transform the sequence: (1) in the subgroup with the smallest element sonmin and other t-1 elements to exchange t-1 times, there is a cost 1:sum + (t-2) * sonmin; (2) with the help of the external element permutation, first let the smallest element minn in the sequence and the smallest element in the group Sonmin Exchange, and then use Minn and other T- 1 elements exchanged t-1 times, then swapped Minn and sonmin back to has cost 2:sum + sonmin + Minn * (T + 1). Reminder: Sum is the value of the current group. So you can count the sum of all the elements, and then each time with min (spend 1, spend 2) can be; for each permutation group of the method, the structure to store the original value and the original value corresponding to the location, and then in ascending order of values to find the location of the transformation. Sequence { { value 3 1 7 10 6 ------> value 1 3 6 7 10 location 1 2 3 4 5 ------> location  2 1 5 3 4 } Code:
#include <cstdio>#include<iostream>#include<cmath>#include<algorithm>#include<cstring>#include<queue>using namespacestd;Const intinf=0x3f3f3f3f;#defineMem (x, y) memset (x,y,sizeof (x))#defineSI (x) scanf ("%d", &x)#definePI (x) printf ("%d", X)typedefLong LongLL;Const intmaxn=10010;intVIS[MAXN];structnode{intPos,val; BOOL operator< (ConstNode &b)Const{ if(Val!=b.val)returnval<B.val; Else returnpos<B.val; }}; Node DT[MAXN];intMain () {intN; while(~SI (N)) {LL sum=0; intMin_all=Inf,min_area; for(intI=1; i<=n;i++) {SI (dt[i].val); Dt[i].pos=i; Sum+=Dt[i].val; Min_all=min (min_all,dt[i].val); } sort (dt+1, dt+n+1); Mem (Vis,0); intnum; for(intI=1; i<=n;i++){ if(!Vis[i]) {num=0; Min_area=Dt[i].val; intj=i; while(!Vis[j]) {Vis[j]=1; Num++; //printf ("%d", dt[j].val);Min_area=min (min_area,dt[j].val); J=Dt[j].pos; }//puts ("");Sum+=min ((num-2) *min_area,2* (Min_all+min_area) + (num-1) *min_all-Min_area); }} printf ("%lld\n", sum); } return 0;}
Cow sorting (permutation group)