python 数组排序最少交换次数_使序列有序的最少交换次数(minimum swaps)

交换相邻两数

如果只是交换相邻两数,那么最少交换次数为该序列的逆序数。

交换任意两数

数字的总个数减去循环节的个数??

A cycle is a set of elements, each of which is in the place of another.  So in example sequences { 2, 1, 4, 3}, there are two cycles: {1, 2} and {3, 4}.  1 is in the place where 2 needs to Go, and 2 is in the place where 1 needs to go 1, so they are a cycle; likewise with 3 and 4.

The sequences {3, 2, 1, 5, 6, 8, 4, 7 }also has two cycles: 3 is in the place of 1 which is in the place of 3, so {1, 3} is a cycle; 2 is in its proper place; and 4 is in the place of 7 which is in the place of 8 in place of 6 in place of 5 in place of 4, so {4, 7, 8, 6, 5} is a cycle.  There are seven elements out of place in two cycles, so five swaps are needed.

实现:

e.g. { 2, 3, 1, 5, 6, 4}

231564 -> 6 mismatch

two cycles -> 123 and 456

swap 1,2 then 2,3, then 4,5 then 5,6 -> 4 swaps to sort

Probably the easiest algorithm would be to use a bitarray. Initialize it to 0, then start at the first 0.

Swap the number there to the right place and put a 1 there.

Continue until the current place holds the right number.

Then move on to the next 0

有序列5,4,3,2,1。共5个数。

nums [0] [1] [2] [3] [4]

5 4 3 2 1

按升序排列之后为

nums1 [0] [1] [2] [3] [4]

1 2 3 4  5

5应该到1处,1应该到5处,形成了一个循环,所以可以将它们抽象成一个环,环内换序就可以了。(这种环称为循环节)如果把它们两个看成整体,对于整个序列来说它们占据了排好序后5,1应该在的位置,所以对于整个序列来说是有序的,

它们只是自身内部无序而已。

上例中3在原本就在的位置,可以看成一个元素的循环节。

我们可以推断出有一个循环节,就可以少交换一次,因为n个元素的循环节,只需交换n-1次即可有序。

那么对于整个序列来说,最少交换次数为 元素总数-循环节个数。Example:

231564

000000

-> swap 2,3

321564

010000

-> swap 3,1

123564

111000

-> continue at next 0; swap 5,6

123654

111010

-> swap 6,4

123456

111111

-> bitarray is all 1's, so we're done.

#include "bits/stdc++.h"

using namespacestd;/** 交换任意两数的本质是改变了元素位置,

* 故建立元素与其目标状态应放置位置的映射关系*/

int getMinSwaps(vector &A)

{//排序

vectorB(A);

sort(B.begin(), B.end());

mapm;int len = (int)A.size();for (int i = 0; i < len; i++)

{

m[B[i]]= i; //建立每个元素与其应放位置的映射关系

}int loops = 0; //循环节个数

vector flag(len, false);//找出循环节的个数

for (int i = 0; i < len; i++)

{if (!flag[i])

{int j =i;while (!flag[j])

{

flag[j]= true;

j= m[A[j]]; //原序列中j位置的元素在有序序列中的位置

}

loops++;

}

}return len -loops;

}

vectornums;intmain()

{

nums.push_back(1);

nums.push_back(2);

nums.push_back(4);

nums.push_back(3);

nums.push_back(5);int res =getMinSwaps(nums);

cout<< res << '\n';return 0;

}

交换任意区间

??

#include "bits/stdc++.h"

using namespacestd;/** 默认目标映射关系是 key 1 => val 1 …… key n => val n

* 如果序列不是 1~n 可以通过 map 建立新的目标映射关系

* 交换任意区间的本质是改变了元素的后继,故建立元素与其初始状态后继的映射关系*/

const int MAXN = 30;intn;intvis[MAXN];intA[MAXN], B[MAXN];intgetMinSwaps()

{

memset(vis,0, sizeof(vis));for (int i = 1; i <= n; i++)

{

B[A[i]]= A[i % n + 1];

}for (int i = 1; i <= n; i++)

{

B[i]= (B[i] - 2 + n) % n + 1;

}int cnt =n;for (int i = 1; i <= n; i++)

{if(vis[i])

{continue;

}

vis[i]= 1;

cnt--;for (int j = B[i]; j != i; j =B[j])

{

vis[j]= 1;

}

}returncnt;

}intmain()

{

cin>>n;for (int i = 1; i <= n; i++)

{

cin>>A[i];

}int res =getMinSwaps();

cout<< res << '\n';return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值