JDOJ-1008: Cattle Sort

Source: Internet
Author: User

1008: Cattle sort time limit:2 Sec Memory limit:64 MB
submit:120 solved:52
[Submit] [Status] [Web Board] Description

Farmer John is ready to queue his n (1 <= n <= 10,000) cows for action. Because a big-tempered bull could be disruptive, John wanted to sort the cows by the size of their temper. Each cow's temper is an integer between 1 and 100,000 and no two cows have the same temper value. During the sorting process, John can swap the positions of any two cows. Because the big-tempered cows are not moving, John needs x+y seconds to exchange two cows with a temper value of x and Y.

Please help John calculate the shortest time to order all the steaks.

Input

Line 1th: A number, N.

Line 2~n+1: One number per line, and line i+1 is the temper value of the first cow.

Output

Line 1th: A number that puts all steaks in good order for the shortest time.

Sample Input3 2 3 1Sample Output7HINT

"Sample description" There are three cows in the queue, with a temper of 2, 3 and 1 respectively.

2 3 1: Initial sequence

2 1 3: Exchange temper for the cattle of 3 and 1 (Time =1+3=4).

1 2 3: Exchange temper for the cattle of 1 and 2 (Time =2+1=3).

Summary: Replace the group naked problem, find each loop, in both cases down to the minimum value, the following quote Hzwer

1. Identify the initial and target states. Clearly, the target State is the sorted state.
2. Draw a permutation group and look for loops inside. For example, the number is 8 4 5 3 2 7
Obviously, the target State is 2 3 4 5 7 8, which can be written as two loops:
(8 2 7) (4 3 5).
3. Observe one of the loops, obviously, to minimize the exchange cost, The smallest number 2 should be used inside the loop, which goes with the additional two numbers, 7 with 8 switching. The cost of this exchange is:
Sum–min + (len–1) * min
after simplification:
sum + (len–2) * min
where Sum of all the numbers for this loop, Len is the length, Min is the smallest number within the ring.

4. Considering the other situation, we can adjust a number from another loop to enter the loop, making the exchange less expensive. For example, the initial state:
1 8 9) 7 6
Can be decomposed into two loops:
(1) (8 6 9 7), obviously, the second loop is (8 6 9 7) and the smallest number is 6. We can deploy the smallest number 1 of the entire sequence into this loop. Change the second loop to: (8 1 9 7). Let this 1 complete the task, then swap with 6, and let 6 go back to the loop. The cost of doing so is clearly:
sum + min + (len + 1) * Smallest
The sum of all the numbers in this loop, Len is the length, Min is the smallest number in the ring, and smallest is the smallest number in the whole sequence.

5. Thus, the price of a loop is Sum–min + (len–1) * min and sum + min + (len + 1) * The small number in the smallest.

#include <bits/stdc++.h>using namespacestd;Const intMAXN =100005;intPOS[MAXN], N, A[MAXN], B[MAXN], tot;BOOLVIS[MAXN];intCNT =0, ans =0, Minx =10000007;voidDfsintNow ) {    if(Vis[now]) {ans+ = min (tot + (CNT-2) * Minx, tot + Minx + (cnt +1) * b[1]); return; } CNT++; Tot + = A[now]; Vis[now] =1; Minx =min (Minx, A[now]); DFS (Pos[a[now]);}intMain () {scanf ("%d", &N);  for(inti =1; I <= N; ++i) scanf ("%d", &a[i]), b[i] =A[i]; Sort (b+1, B + n +1);  for(inti =1; I <= N; ++i) Pos[b[i] =i;  for(inti =1; I <= N; ++i) {if(Vis[i] = =0) {Minx=10000007; CNT = tot =0;        DFS (i); }} printf ("%d\n", ans); return 0;}

JDOJ-1008: Cattle Sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.