交换相邻两数

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

交换任意两数

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

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)的更多相关文章

  1. [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 ...

  2. Java实现 LeetCode 801 使序列递增的最小交换次数 (DP)

    801. 使序列递增的最小交换次数 我们有两个长度相等且不为空的整型数组 A 和 B . 我们可以交换 A[i] 和 B[i] 的元素.注意这两个元素在各自的序列中应该处于相同的位置. 在交换过一些元 ...

  3. [CSL 的魔法][求排序最少交换次数]

    链接:https://ac.nowcoder.com/acm/contest/551/E来源:牛客网题目描述 有两个长度为 n 的序列,a0,a1,…,an−1a0,a1,…,an−1和 b0,b1, ...

  4. [小米OJ] 8. 最少交换次数

    求逆序对数即可 function solution(line) { var nums = line.split(","); var res = 0; for (let i = 0; ...

  5. LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48

    921. 使括号有效的最少添加 921. Minimum Add to Make Parentheses Valid 题目描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的 ...

  6. [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 ...

  7. [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 ...

  8. Cycle Sort (交换次数最少的排序)

    该算法的效率并不高.但是却提供了一个很好的思路.如何让一个序列在最小交换次数下实现有序. Cycle Sort 翻译成中文是 圈排序. 这个圈在于需要交换的数据形成圈. 具体一点: 如: Array ...

  9. 通过交换a,b 中的元素,使[序列a 元素的和]与[序列b 元素的和]之间的差最小

    题目描述: 有两个序列a,b,大小都为n,序列元素的值任意整数,无序:要求:通过交换a,b 中的元素,使[序列a 元素的和]与[序列b 元素的和]之间的差最小.例如:var a=[100,99,98, ...

随机推荐

  1. (二)活用ComponentScan

    项目改造成spring cloud项目后,有非常多组件是复用的,比如(一)敏感信息混淆的组件,比如数据库.Redis等配置, 比如常用的api组件Swagger配置.每个微服务组件里都会有若干个组件随 ...

  2. 微信小程序CheckBox选中事件

    1.微信小程CheckBox选中问题 <checkbox-group bindchange="checkboxChange" data-index="{{index ...

  3. P1414 又是毕业季II (数学?

    题目背景 “叮铃铃铃”,随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业晚会上,相信,这一定 ...

  4. PHP.20-图片上传下载

    图片上传下载 思路: 1.创建图片上传的存放目录 /uploads/ 2.index.php //浏览页面,提供上传表单 上传表单:文件上传必须使用enctype="multipart/fo ...

  5. PHP代码审计2-常用超全局变量,常用命令注入,常用XSS漏洞审计,文件包含

    超全局变量 $GLOBALS — 引用全局作用域中可用的全部变量$_SERVER — 服务器和执行环境信息$_GET — HTTP GET 变量$_POST — HTTP POST 变量$_FILES ...

  6. 7.Mongodb安全性流程

    1.安全性流程 2.超级管理员 为了更安全的访问mongodb,需要访问者提供用户名和密码,于是需要在mongodb中创建用户 采用了角色-用户-数据库的安全管理方式 常用系统角色如下: root:只 ...

  7. CodeForces 873F Forbidden Indices 后缀数组

    忘了当时怎么做的了,先把代码贴上,保存一下后缀数组模板. #include <cstdio> #include <cstring> #include <algorithm ...

  8. Android toolbar menu 字体点击样式

    今天在做toolbar的时候,右边的菜单的点击事件,就是文字,然后文字的样式,文字的大小,文字的颜色,高了半天.最后发现,文字点下去之后是有样式的,也就是按下去有阴影. 哥哥的耐心好,就知道这不是问题 ...

  9. 《Cracking the Coding Interview》——第4章:树和图——题目5

    2014-03-19 04:11 题目:设计算法检查一棵二叉树是否为二叉搜索树. 解法:既然是二叉搜索树,也就是说左子树所有节点都小于根,右子树所有节点都大于根.如果你真的全都检查的话,那就做了很多重 ...

  10. web前端开发总结(未完)

    由于我也是接触前端开发不久,所以呢,自己也会做点小功课,于是,我把前端能够用到的知识稍稍做了下总结,总结的不全面,以后会慢慢完善的! 移动前端开发基础 (总结----待完善)1.移动前端开发:简而言之 ...