使序列有序的最少交换次数(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, ...
随机推荐
- sql xml扩展字段 查询语句
[cms:sql query="SELECT ContentXML.value('/fields[1]/Address[1]','varchar(max)')AS valueForm FRO ...
- linux 安装mysql5.6 yum
安装mysql: 查看mysql: rpm -qa | grep -i mysql 安装必要的环境 yum -y install gcc gcc-c++ ncurses-devel perl 查看环境 ...
- 关于对GitHub的使用
什么是GitHub? GitHub是版本控制和协作的代码托管平台.它可以让你在其他人在任何地方一起工作. 本文主要向您介绍GitHub essentials,如存储库,分支,提交和合并请求.将您创建自 ...
- TouTiao开源项目 分析笔记4==>一个简单APP 整体常用框架
1.效果预览 1.1.如下图所以,到目前为止所有的功能. 2.从InitApp开始->SplashActivity->MainActivity 2.1.InitApp源代码.这是整个项目的 ...
- PHP代码审计5-实战漏洞挖掘-cms后台登录绕过
cms后台登录绕过 练习源码:[来源:源码下载](数据库配置信息有误,interesting) 注:需进行安装 1.创建数据库 2.设置账号密码,连接数据库 3.1 正常登录后台,抓包分析数据提交位置 ...
- WPF开发实例——仿QQ登录界面
原文:WPF开发实例--仿QQ登录界面 版权声明:本文为博主原创文章,如需转载请标明转载地址 http://blog.csdn.net/u013981858 https://blog.csdn.net ...
- EVALUation mode running with code size limit:2k keil进行仿真过程中出现的报错
EVALUation mode running with code size limit:2k 如果keil软件未破解,会限制程序的存储大小.第一是你的软件没有破解,不能编译2K以上的程序:这种情况下 ...
- CentOS6/7-防火墙管理
#CentOS6 #开放端口运行外部访问(不指定源IP) iptables -I INPUT -p tcp --dport -j ACCEPT iptables -I INPUT -p tcp --d ...
- 孤荷凌寒自学python第五十七天初次尝试使用python来连接远端MongoDb数据库
孤荷凌寒自学python第五十七天初次尝试使用python来连接远端MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第三天.感觉这个东西学习起来还是那么困 ...
- hnust 神奇的序列
问题 E: 神奇的序列 时间限制: 1 Sec 内存限制: 128 MB提交: 635 解决: 84[提交][状态][讨论版] 题目描述 Aurora在南宁发现了一个神奇的序列,即对 ...