stu*_*234 5 c++ python sorting algorithm cycle-sort
我们有列表 A,排序后需要看起来像列表 B,并且我们有每个数字的努力或“权重”,所以当我们交换顺序时,努力也会交换,它们是连接的。
知道列表最后应该是什么样子,找出排序列表 A 使其看起来像 lis B 所需的最低努力
我找到了对我的问题的回答,但它在 C++ 代码中位于底部
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
Run Code Online (Sandbox Code Playgroud)
所以当我们搬家的时候
2 和 5 我们将第二个和第五个权重加在一起,所以努力是 3600,列表看起来像这样
a = [1, 4, 2, 3, 6, 5]
Run Code Online (Sandbox Code Playgroud)
总和努力 = 3600
然后我们正在移动 3 和 4 移动的努力再次是 3600 并且看起来像这样
a = [1, 3, 2, 4, 6, 5]
Run Code Online (Sandbox Code Playgroud)
总和努力 = 7200
然后是 1 加 5 所以这次移动的努力是 4000并且一个列表看起来像 b 列表
sum_effort 为 11200
我基于 c++ 所做的
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
Run Code Online (Sandbox Code Playgroud)
a = [1, 4, 2, 3, 6, 5]
Run Code Online (Sandbox Code Playgroud)
我对 python 有点陌生,但如果我不明白这一点,我就不会睡觉