-
Notifications
You must be signed in to change notification settings - Fork 2
/
A1067.cpp
64 lines (58 loc) · 927 Bytes
/
A1067.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 100000 + 10;
int N;
int num[MAXN];
bool judge(){
for(int i = 0; i < N; ++i){
if(num[i] != i) return false;
}
return true;
}
int find(int idx){
for(int i = 0; i < N; ++i){
if(num[i] == idx) return i;
}
//return -1;
}
int find_uneq(){
for(int i = 1; i < N; ++i){
if(num[i] != i) return i;
}
//return -1;
}
void print(){
for(int i = 0; i < N; ++i){
printf("%d ", num[i]);
}
printf("\n");
}
int main(){
int idx;
scanf("%d", &N);
for(int i = 0; i < N; ++i){
scanf("%d", &num[i]);
if(num[i] == 0){
idx = i;
}
}
int ans = 0;
while(!judge()){
if(idx != 0){
int k = find(idx);
//printf("%d %d\n", idx, k);
swap(num[idx], num[k]);
idx = k;
}else{
int k = find_uneq();
//printf("%d %d\n", idx, k);
swap(num[idx], num[k]);
idx = k;
}
//print();
++ans;
}
printf("%d", ans);
return 0;
}