PAT-1067 Sort with Swap(0,*)

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 (<=105) 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

Topic

Given a sequence of N numbers, the numbers are in any order from 0 to N-1, and the sequence is required to be sorted by exchanging 0 with another number, and the minimum number of exchanges is required.

Problem solving ideas

1. One idea is to exchange directly and calculate the total number of exchanges.

  • When the 0 is not in the 0 position, if the 0 is in the i position, the 0 should be swapped with the number i;
  • When 0 is at the 0 position and the array is not in order, first swap 0 with the first unmatched number, and then continue with the previous operation.

To prevent timeouts, the following points should be noted:

  • When receiving input, two arrays should be created, one to store the value and the other to store the index of the corresponding value;
  • When receiving the sequence, it should record how many numbers other than 0 do not match (not in the corresponding position), and this value will be used as the judgment value for the end of the loop sorting. Because every valid exchange (that is, 0 is not in the 0 position) will have a number returned to its original position, when the number of mismatches is 0, it means that the sorting is completed;
  • When 0 is in the 0th position, you need to find the first number that is not in the corresponding position. If you traverse from the beginning every time, it will time out. At this time, you should record the position you find each time, and continue to traverse from there next time, because the previous All the numbers of , can already be determined to be "in their own place".

2. Another idea is to find the cycles in the sequence and thus calculate the required number of swaps without the actual swap operation.

  • Contains 0 rings, and the number of exchanges is the number of non-zero elements in the ring;
  • Excluding the 0 ring, the number of exchanges is the number of elements in the ring + 1, because 0 is in the 0 position at this time, it is necessary to exchange 0 into the ring to form a ring containing 0.
#include <cstdio>
using namespace std;
int a[100000];
int index[100000];

void swap(int x, int y) {
    int t = a[x];
    a[x] = a[y];
    a[y] = t;
    index[a[x]] = x;
    index[a[y]] = y;
}

int main(void) {
    int n;                      //数字个数
    int t = 0;                  //需要进行交换的数的个数
    int count = 0;              //swap()交换次数

    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int num;
        scanf("%d", &num);
        a[i] = num;
        index[num] = i;
        if (num != i && num != 0)
            t++;
    }
    int k = 1;                  //第一个不匹配的数的索引
    while (t > 0) {
        if (a[0] == 0) {
            for (int i = k; i < n; i++) {
                if (i != a[i]) {
                    k = i + 1;
                    swap(0, i);
                    count++;
                    break;
                }
            }
        }
        swap(index[0], index[index[0]]);
        t--;
        count++;
    }
    printf("%d\n", count);

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325935453&siteId=291194637