-
Notifications
You must be signed in to change notification settings - Fork 0
/
1067.cpp
86 lines (86 loc) · 1.15 KB
/
1067.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<stdio.h>
#define DEBUG
int a[100000];
int dict[100000];
int main()
{
int n,i,p;
int count=0;
int wellp=1;
scanf("%d",&n);
for(i=0;i<n;i++)
{
int t;
scanf("%d",&t);
a[i]=t;
dict[t]=i;
}
for(i=0;i<n;i++)
if(a[i]==0)
{
p=i;
break;
}
#ifdef DEBUG
printf("a:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
printf("dict:\n");
for(i=0;i<10;i++)
printf("%d ",dict[i]);
printf("\n");
printf("p=%d\n",p);
printf("\n");
#endif
while(wellp<n)
{
while(p!=0)
{
i=dict[p];
a[p]=p;
dict[p]=p;
p=i;
dict[0]=i;
a[i]=0;
count++;
#ifdef DEBUG
printf("a:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
printf("dict:\n");
for(i=0;i<10;i++)
printf("%d ",dict[i]);
printf("\n");
printf("p=%d\n",p);
printf("\n");
#endif
}
while(wellp<n && a[wellp]==wellp)
wellp++;
if(wellp<n)
{
a[0]=a[wellp];
dict[a[wellp]]=0;
p=wellp;
dict[0]=p;
a[p]=0;
count++;
#ifdef DEBUG
printf("a:\n");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
printf("dict:\n");
for(i=0;i<10;i++)
printf("%d ",dict[i]);
printf("\n");
printf("p=%d\n",p);
printf("\n");
#endif
}
}
printf("%d",count);
return 0;
}