The main topic is:
Each cow has a corresponding value of a[i], and now given an initial sequence of cows, hoping to pass a 22 exchange, can make these cows in ascending order of values, each exchange will consume a a[i]+a[j]
Hope to minimize the cost and to find out the minimum cost
Personally think this problem is quite interesting, although I wa a lot of hair, but it is worth thinking about
This is a permutation group problem, but we first have to determine the position of each cow according to its value, and then get a mapping relationship according to the position where we are now to[i]
Save A[i] with b[] array, sorted, b[i] denotes the value of the ox of the first size
We find out all the loop sets in this permutation group, each of which is discussed separately, we always find the smallest cow in the loop set to occupy the position of the other bull POS, and then exchange this POS position to the corresponding cow
This is b[min]+b[pos], because every cow always go back to his position, assuming that the cycle of the concentration of Len a cow, then all the cattle back to their position so all the b[i] will be added again
But the process of adding is expected to be as small as possible, so always add to b[min, this is guaranteed, because in a loop set, each Exchange can only make one number return to the correct position, unless the last step
, otherwise, if you return to the correct position at the same time, then these two numbers can be separated as a cyclic set, which is contradictory to them in the original loop set
In fact, it feels like the whole idea is right.
But I'm here in WA.
I've been thinking about it for a while.
There is also a situation to consider is that the circulation of the central location of the exchange can use B[1] (that is, the smallest cattle) to help
is not the result even smaller by using the smallest number of cows to help the loop set up the queue?
The smallest cow added to help is the cow with the value b[1] and the smallest cow in the current cycle set to exchange a position, help and then Exchange back
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 Const intN =10005;7 Const intM =100005;8 intA[n], B[n], to[n], vis[n], id[m];//to[] Record mapping relationships on permutation groups9 Ten intCircleintu) One { A intv = u, ans =0; - intCNT =0; - while(U! =To[v]) { theVIS[V] =1; -cnt++; -Ans = ans+B[v]; -v =To[v]; + } -Ans = ans+b[v], cnt++; +VIS[V] =1; A if(CNT = =1)return 0; at Else{ - /*here to determine whether to use the smallest number of cows to help the loop set the team is not the result of a smaller - the smallest cow added to help is the cow with the value b[1] and the smallest cow in the current cycle set to swap positions once, helping - and swap it back .*/ - intTMP = ans + b[u] + (cnt+1) *b[1]; - returnMIN (tmp, ans + (cnt-2)*B[u]); in } - } to + intMain () - { the //freopen ("a.in", "R", stdin); * intN; $ while(SCANF ("%d", &n)! =EOF)Panax Notoginseng { - for(intI=1; I<=n; i++) the { +scanf"%d", A +i); AB[i] =A[i]; the } +Sort (b +1, b+n+1); - for(intI=1; I<=n; i++) $Id[b[i]] =i; $ for(intI=1; I<=n; i++) -To[i] = id[a[i]];//indicates that the first position is occupied by the cattle of the id[a[i]] . -memset (Vis,0,sizeof(Vis)); the intAns =0; - for(intI=1; I<=n; i++)Wuyi if(!vis[i]) ans + =Circle (i); the -printf"%d\n", ans); Wu } - return 0; About}
POJ 3270 Permutation group problem