• 使序列有序的最少交换次数(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 << '
    ';
    
        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 << '
    ';
    
        return 0;
    }
  • 相关阅读:
    url 记录
    tvm
    const flold
    spring
    java连接mysql数据库
    linux常用命令记录
    pikachu漏靶场洞测试
    Starting.....
    IOS App提交流程
    InApp Purchase(iap)快速指南
  • 原文地址:https://www.cnblogs.com/kimsimple/p/6883572.html
Copyright © 2020-2023  润新知