We have list A that after sorting needs to look like list B and we have effort or "weight" of each number so when we are swapping in order effort will swap also ,they are connected.
Knowing how list should look like at the end find what is the lowest effort needed to sort list A to look like lis B
I've found answear to my question but it's in c++ code is at the bottom
6 <--- how many numbers there is
w = [2400, 2000, 1200, 2400, 1600, 4000] <----- effort
a = [1, 4, 5, 3, 6, 2] <----- starting list
b = [5, 3, 2, 4, 6, 1] <----- how it should be sorted
so when we are moving
2 and 5 we are taking second and fifth weight and add them together so effort is 3600 and lists looks like this
a = [1, 4, 2, 3, 6, 5]
sum_effort = 3600
then we are moving 3 and 4 effort of this move is again 3600 and a looks like this
a = [1, 3, 2, 4, 6, 5]
sum_effort = 7200
and then 1 with 5 so effort of this move is 4000 and a list looks like b list
sum_effort is 11200
what I did based on c++
weight = [2400, 2000, 1200, 2400, 1600, 4000]
og = [1, 4, 5, 3, 6, 2]
position = [5, 3, 2,4, 6, 1]
result = 0
for x in range(len(og)):
suma = 0
min_cycle = 0
len_cycle = 0
current = x
while(1):
min_cycle = min(min_cycle, weight[current])
suma = suma + weight[current]
current = position[current]
len_cycle += 1
if current == x:
break
result += min(suma+(len_cycle-2)*min_cycle, suma+min_cycle+(len_cycle+1)*min_weight)
print(result)
#include <cstdio>
#include <algorithm>
#define REP(a,n) for (int a=0; a<(n); ++a)
using namespace std;
#define INF 1000000000
typedef long long LL;
///////////////////////////
#define MAXN 1000000
int wagi[MAXN];
int orig[MAXN]; // orgin
int perm[MAXN]; // end_pos
bool vis[MAXN];
int minw = INF; // minimalna waga
int main()
{
int N;
scanf("%d", &N);
REP(a, N)
{
scanf("%d", &wagi[a]);
minw = min(minw, wagi[a]);
}
REP(a, N)
{
scanf("%d", &orig[a]);
--orig[a];
}
REP(a, N)
{
int nr;
scanf("%d", &nr);
--nr;
perm[nr] = orig[a];
}
LL wynik = 0;
REP(pocz, N)
if (!vis[pocz])
{
int minc = INF;
LL suma = 0;
int cur = pocz;
int dl = 0;
for (;;)
{
minc = min(minc, wagi[cur]);
suma += wagi[cur];
cur = perm[cur];
vis[cur] = true;
++dl;
if (cur==pocz)
break;
}
wynik += min(suma+(dl-2)*(LL)minc, suma+minc+(dl+1)*(LL)minw);
}
printf("%Ld\n", wynik);
}
Im kinda new to python but I won't go to sleep if i don't figure this out