Sorting cost (index sorting)

Total time limit: 10000ms
single test point time limit: 5000ms
Memory limit: 65536kB

Describe the
existing row of boxes full of goods, with different weights (all have been marked on the boxes). In order to carry out the following work, these boxes need to be placed in an orderly manner, but only one staff member is responsible for this work , Due to limited space, he can only achieve the sorting of boxes by constantly swapping the positions of two boxes (not adjacent). He knows that this will definitely accomplish the task, but it is very tiring to carry the boxes, so he hopes to find the most labor-saving way to complete the work. Assuming that the cost of exchanging two boxes at a time is the sum of the weight of the two boxes, then this work The total cost of this is the sum of all "exchange" costs in the process. Please help him calculate the minimum cost of arranging these boxes.

The input
input contains multiple data instances, each instance occupies a row, and each row of data is a non-negative integer separated by spaces. The first integer represents the number of boxes (n), and the next is n different positive integers. Represents the weight of n boxes respectively, and the order indicates the initial order of the boxes; when n=0, the input ends.

Output
For each valid data instance (n!=0), an integer is output, on a line alone, indicating the minimum cost of sorting boxes in this instance. Sample input 3 3 2 1 4 8 1 2 4
5 1 8 9 7 6 6 8 4 5 3 2 7 0 Sample output 4 17 41 34

Prompt
Example explanation: There are 4 valid data instances: the
first data instance is 3 2 1. Just by swapping boxes with weights 3 and 1, the total cost is 3+1=4; the second data instance is 8 1 2 4. The exchange process with the
least cost is 1↔2, 1↔4, 1↔8, and the total cost is (1+2)+(1+4)+(1+8) = 17; the third data instance is 1 8 9 7 6. The least costly exchange process is
1↔6, 1↔9, 1↔7, 1↔8, 1↔6, and the total cost is (1+6)+(1+9)+(1+ 7)+(1+8)+(1+6)=41; The fourth data instance is 8 4
5 3 2 7. The least costly exchange process is 3↔5, 3↔4, 2↔7, 2↔ 8. The total cost is (3+5)+(3+4)+(2+7)+(2+8)=34.

Please note the boundary conditions and IO, n may be very large.

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 1000005;
const int INF = 0x7fffffff;
int a[MAXN], idx[MAXN], vis[MAXN];
int n;

bool cmp(int i, int j) {
    
    
    return a[i] < a[j];
}

int cnt = 0;
int cur_min = INF; /* min element on the cycle */

/**
 * @return sum of elements on the cycle 
 */
int dfs(int cur)
{
    
    
    vis[cur] = 1;
    ++cnt;
    cur_min = min(cur_min, a[cur]);
    if (!vis[idx[cur]]) {
    
    
        int s = a[cur] + dfs(idx[cur]);
        return s;
    }
    else 
        return a[cur];
}

int main()
{
    
    
    while (scanf("%d", &n) == 1) {
    
    
        if (n == 0)
            break;
        memset(vis, 0, sizeof(vis));
        int minv = INF; /* min element of whole array */
        for (int i = 0; i < n; ++i) {
    
    
            scanf("%d", &a[i]);
            idx[i] = i;
            minv = min(minv, a[i]);
        }
        sort(idx, idx+n, cmp);
        int ans = 0;
        for (int i = 0; i < n; ++i) 
            if (!vis[i]) {
    
    
                cnt = 0, cur_min = INF;
                /* 
                 * Choice 1: swap using the min element of current cycle, cnt-1 round in total
                 * Choice 2: swap using the min element of whole array, cnt+1 round in total(1 more to initiaze, 1 more to restore)
                 */
                ans += dfs(i) + min((cnt-2)*cur_min, (cnt+1)*minv+cur_min);
            }
        printf("%d\n", ans);
    }
    system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/w112348/article/details/111026057