#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#define swap(a, b) {int c = a; a = b; b = c;}
const int N = 100000;
int find(int arr[], int n);
int k = 1;
int main(void) {
int arr[N], i, n, tmp, index;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &tmp);
arr[tmp] = i;
}
int cnt = 0;
while (true) {
if (!arr[0]) {
index = find(arr, n);
if (index == -1) {
break;
}
swap(arr[0], arr[index]);
cnt++;
}
else {
index = arr[0];
swap(arr[0], arr[index]);
cnt++;
}
}
printf("%d\n", cnt);
return 0;
}
int find(int arr[], int n) {
for (; k < n; k++) {
if (arr[k] != k) {
return k;
}
}
return -1;
}
1067. Sort with Swap(0,*) (25)解题报告
最新推荐文章于 2021-03-18 00:22:51 发布