交换相邻两数

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

交换任意两数

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

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. Python__for循环和列表生成式的区别

    话不多,上例子 >>> L = [,,] >>> for i in range(len(L)): L[i] = L[i] + L[i-] print(L) #结果 ...

  2. tp5 使用技巧(持续更新中...)

    tp5 使用技巧(持续更新中...) 1.自动写入时间 create_time和update_time 使用save方法才行,insert方法不生效,不知为何 2.过滤字段 allowfield和st ...

  3. array_unique() - 去除数组中重复的元素值

      array_unique() 定义和用法 array_unique() 函数移除数组中的重复的值,并返回结果数组. 当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除. 返回的数组中键名 ...

  4. linux学习(3)——vim文本编辑工具

    (三) vi与vim的最大区别就是编辑一个文本vi不显示颜色,vim显示颜色. 安装: yum install -y vim-enhance Vim有三种模式 A:一般模式 上下左右光标 k j h ...

  5. python 学习心得

    多用类或是函数 我以前写的时候,不用函数,从头写到尾,后来,要改成函数,也是要花很多测试的时间,改的话还得一个调试,如果一开始就用函数的话,就能节省很多时间. 函数的功能尽可能的小 比如说像我用try ...

  6. Smail 中的一些点

    smali中所有操作都需要经过寄存器, 本地寄存器以v开头, 参数寄存器以p开头, 非static方法中p0是this 没有-object后缀的操作指令表示操作的对象是基本类型 invoke-dire ...

  7. html_outputer.py

    coding=UTF-8 # HTML输出器 import sys class htmlOutputer(): def __init__(self): self.data = [] def colle ...

  8. 阿里巴巴Java开发规约Eclipse插件安装及使用

    技术交流群:233513714 插件安装 环境:JDK1.8,Eclipse4+.有同学遇到过这样的情况,安装插件重启后,发现没有对应的菜单项,从日志上也看不到相关的异常信息,最后把JDK从1.6升级 ...

  9. 开启虚拟机所报的错误:VMware Workstation cannot connect to the virtual machine. Make sure you have rights to run the program, access all directories the program uses, and access all directories for temporary fil

    当我们开启虚拟机时出现错误: VMware Workstation cannot connect to the virtual machine. Make sure you have rights t ...

  10. 《Cracking the Coding Interview》——第2章:链表——题目1

    2014-03-18 02:16 题目:给定一个未排序的单链表,去除其中的重复元素. 解法1:不花额外空间,使用O(n^2)的比较方法来找出重复元素. 代码: // 2.1 Remove duplic ...