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
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 int num[100001]; 6 int main(){ 7 int N, temp, index = 1, ans = 0, cnt = 0; 8 scanf("%d", &N); 9 for(int i = 0; i < N; i++){ 10 scanf("%d", &temp); 11 num[temp] = i; 12 if(temp != 0 && temp != i) 13 cnt++; 14 } 15 while(cnt > 0){ 16 if(num[0] == 0){ 17 for(int i = index; i < N; i++){ 18 if(num[i] != i){ 19 index = i; 20 break; 21 } 22 } 23 swap(num[0], num[index]); 24 ans++; 25 continue; 26 }else{ 27 swap(num[0], num[num[0]]); 28 ans++; 29 cnt--; 30 } 31 } 32 printf("%d", ans); 33 cin >> N; 34 return 0; 35 }
总结:
1、题意:按照示例所说,用0与其他元素交换位置,使得被交换的元素到达正确的位置。不断进行,直到所有元素都归位。
2、在0与其他元素交换的过程中,会出现0被交换到0的位置,后续无法继续交换的情况,但有可能整个序列还未调整完。这时需要找一个未归位的元素,将其与0交换,使0乱序,再继续正常进行交换流程。找未归位元素的过程不能每次都从头开始,否则复杂度会变为n^2。可以设置一个下标index初值为1,其左边的元素都已经有序,所以每次只需从index开始寻找。
3、设置一个计数器,该计数器记录当前不在本位的元素的个数(初始化可在读入数据时完成)。进行一次交换并有一个元素归位时计数器减一。当计数器为0时完成排序。
4、在num[ ]数组中,如果用数组内容表示数字,数组下标表示位置,则每次0与一个元素交换时,都需要遍历一次数组以找到该元素的位置,导致复杂度为n^2。但如果用数组下标表示数字,数组内容表示数字的位置,则可以避免这种情况。
5、当输入的数据过万时,就要注意避免n^2复杂度。