使序列有序的最少交换次数(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 namespace std;
/*
* 交换任意两数的本质是改变了元素位置,
* 故建立元素与其目标状态应放置位置的映射关系
*/
int getMinSwaps(vector<int> &A)
{
// 排序
vector<int> B(A);
sort(B.begin(), B.end());
map<int, int> m;
int len = (int)A.size();
for (int i = ; i < len; i++)
{
m[B[i]] = i; // 建立每个元素与其应放位置的映射关系
} int loops = ; // 循环节个数
vector<bool> flag(len, false);
// 找出循环节的个数
for (int i = ; i < len; i++)
{
if (!flag[i])
{
int j = i;
while (!flag[j])
{
flag[j] = true;
j = m[A[j]]; // 原序列中j位置的元素在有序序列中的位置
}
loops++;
}
}
return len - loops;
} vector<int> nums; int main()
{
nums.push_back();
nums.push_back();
nums.push_back();
nums.push_back();
nums.push_back(); int res = getMinSwaps(nums); cout << res << '\n'; return ;
}
交换任意区间
??
#include "bits/stdc++.h"
using namespace std;
/*
* 默认目标映射关系是 key 1 => val 1 …… key n => val n
* 如果序列不是 1~n 可以通过 map 建立新的目标映射关系
* 交换任意区间的本质是改变了元素的后继,故建立元素与其初始状态后继的映射关系
*/
const int MAXN = ; int n;
int vis[MAXN];
int A[MAXN], B[MAXN]; int getMinSwaps()
{
memset(vis, , sizeof(vis)); for (int i = ; i <= n; i++)
{
B[A[i]] = A[i % n + ];
}
for (int i = ; i <= n; i++)
{
B[i] = (B[i] - + n) % n + ;
} int cnt = n;
for (int i = ; i <= n; i++)
{
if (vis[i])
{
continue;
}
vis[i] = ;
cnt--;
for (int j = B[i]; j != i; j = B[j])
{
vis[j] = ;
}
} return cnt;
} int main()
{
cin >> n;
for (int i = ; i <= n; i++)
{
cin >> A[i];
} int res = getMinSwaps(); cout << res << '\n'; return ;
}
使序列有序的最少交换次数(minimum swaps)的更多相关文章
- [Swift]LeetCode801. 使序列递增的最小交换次数 | Minimum Swaps To Make Sequences Increasing
We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...
- Java实现 LeetCode 801 使序列递增的最小交换次数 (DP)
801. 使序列递增的最小交换次数 我们有两个长度相等且不为空的整型数组 A 和 B . 我们可以交换 A[i] 和 B[i] 的元素.注意这两个元素在各自的序列中应该处于相同的位置. 在交换过一些元 ...
- [CSL 的魔法][求排序最少交换次数]
链接:https://ac.nowcoder.com/acm/contest/551/E来源:牛客网题目描述 有两个长度为 n 的序列,a0,a1,…,an−1a0,a1,…,an−1和 b0,b1, ...
- [小米OJ] 8. 最少交换次数
求逆序对数即可 function solution(line) { var nums = line.split(","); var res = 0; for (let i = 0; ...
- LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48
921. 使括号有效的最少添加 921. Minimum Add to Make Parentheses Valid 题目描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的 ...
- [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- [LeetCode] Minimum Swaps To Make Sequences Increasing 使得序列递增的最小交换
We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...
- Cycle Sort (交换次数最少的排序)
该算法的效率并不高.但是却提供了一个很好的思路.如何让一个序列在最小交换次数下实现有序. Cycle Sort 翻译成中文是 圈排序. 这个圈在于需要交换的数据形成圈. 具体一点: 如: Array ...
- 通过交换a,b 中的元素,使[序列a 元素的和]与[序列b 元素的和]之间的差最小
题目描述: 有两个序列a,b,大小都为n,序列元素的值任意整数,无序:要求:通过交换a,b 中的元素,使[序列a 元素的和]与[序列b 元素的和]之间的差最小.例如:var a=[100,99,98, ...
随机推荐
- 使用union all 命令之后如何对hive表格进行去重
业务场景大概是这样的,这里由两个hive表格,tableA 和 tableB, 格式内容都是这样的: uid cate1 cate2 在hive QL中,我们知道union有着自动去重的功能,但是那是 ...
- Git使用简单总结--源自廖雪峰网站
创建版本库git add加入到暂存区git commit -m" "加入到分支 时光机穿梭git satus查看仓库的当前状态git diff file 查看修改内容 版本回退HE ...
- HTML中body相关标签-02
今日内容: 字体标签: h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub> ...
- Java线程和多线程(六)——守护线程
当我们在Java中创建线程的时候,这个线程在默认的情况下是一个用户线程,并且,如果这个线程在运行,那么JVM就不会终结这个应用.和用户线程不同,当一个线程被标记为守护线程的时候,JVM在用户线程结束的 ...
- Java线程---简介
实现方式 在java中线程有两种实现方式: 1.继承thread类 2.实现runnable接口 Thread类 Thread类时在java.lang包中定义的,继承thread类必须重写run()方 ...
- springboot 采用HttpClient获取天气预报 异常及原因
采用httpClient调用天气预报地址获取出现异常 2018-10-04 15:18:25.815 ERROR 10868 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[ ...
- (A)eclipse搭建springboot项目入门
网上许多资料都是用idea的,但是我个人用eclipse习惯了,所以就在eclipse里面自己尝试着写了一个hello. 然而项目建好后却迟迟不能访问!!!网上搜了许多资料都不靠谱! 虽然最后能看到h ...
- CSS系列(5)-如何使用Firebug查看网页的html和css
Firebug是火狐浏览器Firefox的一个插件,专门为开发人员开发的.使用Firebug需要先在Firefox中安装这个插件,网上有很多教程,可以对照着安装一下. 不同的火狐浏览器版本中的Fire ...
- Python 推导式推导序列
推导式是从一个或多个迭代器快速创建序列的方法.它可以将循环和条件判断结合,从而避免冗长的代码. 一.列表推导式 语法: [表达式 for item in 可迭代对象] [表达式 for item in ...
- HTTP响应码
更详细的内容参考:https://baike.baidu.com/item/HTTP状态码/5053660?fr=aladdin 转载CSDN作者:ddhsea 的文章 https://blog.cs ...