PAT A1067 Sort with Swap(0,*) (25)

Given any permutation of the numbers {0, 1, 2,…, N-1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:

Swap(0, 1) => {4, 1, 2, 0, 3}\ Swap(0, 3) => {4, 1, 2, 3, 0}\ Swap(0, 4) => {0, 1, 2, 3, 4}

Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.

Input Specification:

Each input file contains one test case, which gives a positive N (<=10^5^) followed by a permutation sequence of {0, 1, …, N-1}. All the numbers in a line are separated by a space.

Output Specification:

For each case, simply print in a line the minimum number of swaps need to sort the given permutation.

Sample Input:

10 3 5 7 2 6 4 9 0 8 1

Sample Output:

9

解题思路

  1. 按照题中描述模拟一遍,可以发现,每次“0”都与一个没有归位的数字交换,而且这个数字的最终位置就是“0”所在的位置。
  2. 中间会出现“0”回到原位的情况,这是只要和一个未归位的数字再交换一下,继续即可。 所以,思路大体上就是交换并记录次数。
  3. 数据的存储有一点技巧,由于题设给定,数字是连续的,所以存储时存的是当前数字所在的位置。这样在后面查找以及交换时非常方便。
  4. Swap可以使用Algorithm中的,也可以自己写。

AC代码

#include <cstdio>
#include <algorithm>
using namespace std;
// 排序 只能通过和 0 交换位置
const int maxn = 100010;
int pos[maxn];  // 存放当前数字所在的位置

int main() {
    int a, num;
    scanf("%d", &num);

    int left = num - 1; //还剩余多少数字未归位
    for (int i = 0; i < num; i++)
    {
        scanf("%d", &a);
        pos[a] = i;
        if (a == i && a != 0)
            left--;
    }
    int count = 0,  x = 1;
    while (left > 0)
    {
        if (pos[0] == 0)        // 若 0 回到了第一位,但还没有排好序,和第一个没归位的数字换一下
        {
            while (x < num) {   // 找到第一个未归位的数字
                if (pos[x] != x)
                {
                    swap(pos[0], pos[x]);
                    count++;
                    break;
                }
                x++;
            }
        }
        //Swap
        while (pos[0] != 0)
        {
            swap(pos[0], pos[pos[0]]);
            count++;
            left--;
        }
    }
    printf("%d\n", count);

    return 0;
}

下面再贴一个一开始采取的常规存储方式超时的算法。

#include <cstdio>
#include <algorithm>
using namespace std;
// 排序 只能通过和 0 交换位置
const int maxn = 100010;
int num, n[maxn], m[maxn];
bool flag[maxn];    // 数字为0~n-1

bool cmp(int a[], int b[]) {
    for (int i = 0; i < num; i++)
        if (a[i] != b[i]) return false;
    return true;
}

int main() {
    scanf("%d", &num);
    for (int i = 0; i < num; i++)
    {
        scanf("%d", &n[i]);
        m[i] = n[i];
    }
    sort(m, m + num);   // 辅助数组排序

    int count = 0, pointer0 = 0, pointerN = 0, x = 1;   // pointer0为0的位置
    while (n[pointer0++] != 0); // 找到0的位置
    pointer0--;
    while (!cmp(n, m))
    {
        if (pointer0 == 0)      // 若 0 回到了第一位,但还没有排好序,和第一个没归位的数字换一下
        {
            while (flag[n[x++]]);   // 找到第一个未归位的数字
            int t = n[pointer0];
            n[pointer0] = n[x - 1];
            n[x - 1] = t;
            count++;
        }
        while (n[pointerN++] != pointer0);      // 找到 0 所在位置的数字
        pointerN--;
        flag[n[pointerN]] = true;               // 标记此数以归位
        //Swap
        int temp = n[pointer0];
        n[pointer0] = n[pointerN];
        n[pointerN] = temp;
        pointer0 = pointerN;
        pointerN = 0;
        count++;
    }
    printf("%d\n", count);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/fried123123/article/details/81517769