/* For each group, we have two types of hair exchange: 1. in the group for, take the smallest number t in the group and the other each number exchange, a total of K-1 times, the cost is: Sum + (K-2) * t.2. will be the smallest number of the series Minn, pulled into the group, exchange the smallest number of T with the group, then exchange the K-1 with the smallest number of others, and then replace the Minn with the T, which costs: sum + T + (k + 1) * Minn so minimum cost we take the smallest of the two, that is, sum + min {(K-2) * t, t + (k + 1) * Minn }. */# include <stdio. h> # include <algorithm> # include <string. h ># include <iostream> using namespace STD; struct node {int ID; int num ;}; struct node A [10010]; bool CMP (node A1, node A2) {return a1.num <a2.num ;} Int main () {int sum1, I, Minn, N; int B [10010], C [10010]; while (~ Scanf ("% d", & N) {sum1 = 0; Minn = 100010; for (I = 1; I <= N; I ++) {scanf ("% d", & B [I]); A [I]. num = B [I]; A [I]. id = I; sum1 + = B [I]; If (B [I] <Minn) Minn = B [I]; C [I] = I ;} sort (a + 1, A + n + 1, CMP); for (I = 1; I <= N; I ++) {int T; if (B [I]! = 0) {int COUNT = 1; t = B [I]; int d = A [C [I]. ID; If (B [d] <t) t = B [d]; while (D! = I) {count ++; D = A [D]. ID; If (B [d] <t) t = B [d];} int v = (count-2) * t; int W = (count + 1) * Minn + T; sum1 + = min (V, W); B [I] = 0 ;}} printf ("% d \ n", sum1 );} return 0 ;}
Poj 3270 cow sorting (replacement group)