交换相邻两数

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

交换任意两数

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

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. Redis------Set集合类型

    set是string类型的无序集合 类比:你的朋友不能超过2的32次方-1个元素 基本添加删除操作 取并集 取交集 取差集 注意:每个元素的各个元素不能重复 应用场合:qq好友推荐‘ TomFri 的 ...

  2. iptables v1.3.5: multiple -d flags not allowed错误已解决

    今天写了一条iptables的规则 iptables -t filter -A INPUT -s 192.168.192.0/24 -d 192.168.192.140 -p tcp -dport 2 ...

  3. 操作视频-对视频进行canny边缘检测

    #include<opencv2/opencv.hpp> using namespace cv; int main() { VideoCapture capture(); //从摄像头读入 ...

  4. js简单的获取与输出

    js获取标签内容和输出内容到页面 获取: html: <select id="choiceSelect" onchange="changeImg()"&g ...

  5. HTML标准开头

    <!doctype html> <html>   <head> <meta charset="utf-8"> <title&g ...

  6. codility

    // you can also use imports, for example: // import java.util.*; // you can write to stdout for debu ...

  7. “管中窥豹”,MyCAT的基因缺陷

    提起MyCAT,我的脑海里,总是浮现出这样一首偈.弘忍觉得自己老了,需要找一个接班人,于是,弘忍要求弟子们每人写一个偈子,根据偈子观察各位弟子的开悟程度,从而传授正宗禅宗的衣钵.弟子们心里其实都很明白 ...

  8. 剑指Offer - 九度1521 - 二叉树的镜像

    剑指Offer - 九度1521 - 二叉树的镜像2013-11-30 23:32 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入 ...

  9. Python3中文教程

    搜索 此文档来源自网络 安装 PYTHON❝ Tempora mutantur nos et mutamur in illis. (时光流转,吾等亦随之而变.) ❞ — 古罗马谚语 深入欢迎来到 Py ...

  10. 孤荷凌寒自学python那些事第二天

    孤荷凌寒自学python第二天 Python的变量声明 (完整学习过程屏幕记录视频在文末,手写笔记在文末) Python的变量声明不必要显式指定变量类型 甚至也不需要进行显式的声明 比javascri ...