使序列有序的最少交換次數(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 = 0; i < len; i++)
    {
        m[B[i]] = i;    //  建立每個元素與其應放位置的映射關系
    }

    int loops = 0;      //  循環節個數
    vector<bool> flag(len, false);
    //  找出循環節的個數
    for (int i = 0; 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(1);
    nums.push_back(2);
    nums.push_back(4);
    nums.push_back(3);
    nums.push_back(5);

    int res = getMinSwaps(nums);

    cout << res << '\n';

    return 0;
}

交換任意區間

??

#include "bits/stdc++.h"
using namespace std;
/*
 *  默認目標映射關系是 key 1 => val 1 …… key n => val n
 *  如果序列不是 1~n 可以通過 map 建立新的目標映射關系
 *  交換任意區間的本質是改變了元素的后繼,故建立元素與其初始狀態后繼的映射關系
 */
const int MAXN = 30;

int n;
int vis[MAXN];
int A[MAXN], B[MAXN];

int getMinSwaps()
{
    memset(vis, 0, sizeof(vis));

    for (int i = 1; i <= n; i++)
    {
        B[A[i]] = A[i % n + 1];
    }
    for (int i = 1; i <= n; i++)
    {
        B[i] = (B[i] - 2 + n) % n + 1;
    }

    int cnt = n;
    for (int i = 1; i <= n; i++)
    {
        if (vis[i])
        {
            continue;
        }
        vis[i] = 1;
        cnt--;
        for (int j = B[i]; j != i; j = B[j])
        {
            vis[j] = 1;
        }
    }

    return cnt;
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> A[i];
    }

    int res = getMinSwaps();

    cout << res << '\n';

    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM