Cow Sorting (permutation Group sort)
time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 6909 |
|
Accepted: 2716 |
Description Farmer John ' s n (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 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 whos E grumpiness levels is 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:lineI+1 describes the grumpiness of cowI.
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).Source Usaco February Gold
Main Topic:
John's Ranch has n cows, and each cow has a rank level, guaranteeing no cattle of the same grade. Two cows with a known exchange level of a, A and B are spent a+b Q. How much does it cost to be able to rank n cows as a sequence that increments from left to right? The original replacement can also play this way! Rise Posture First consider turning the original sequence into several permutations. Such as 3 4 1 5 2--(31) (425) It is known that the displacement is not affected, so for each permutation, it is necessary (len-1) to return all the bits (len is the displacement length). So take This. min. min to the rest of the cattle the cost must be minimal, for sum+min* (len-1) In addition, there is a situation that may be more optimal: Take out all cows in the lowest rank of small, with the minimum value in the current permutation min Exchange location, spend small+min, in fact, is the interchange of the replacement, and then the addition of the cattle in the order, the cost of small* (len-1) + (sum-min) Then interchange with min, spend Small+min Total cost small* (len+1) +sum+min Take a minimum of two cases. As for the processing sequence, I sort it out after marking each cow where it should be, and then each permutation is done separately. |