-
Notifications
You must be signed in to change notification settings - Fork 0
/
11212.cpp
78 lines (71 loc) · 1.44 KB
/
11212.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
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int list[10],chd[10];
int maxd;
int ans;
int right(){
int right = 0;
for(int i = 0; i < n; i++)
if(chd[i] == i + 1) right++;
return right;
}
void move(int begin, int end, int pos){
int pre, after, cur = 0, b, e;
for(int i = 0; i < n; i++){
list[i] = chd[cur];
cur = chd[cur];
}
b = list[begin];
e = list[end];
if(!begin) pre = 0;
else pre = list[begin - 1];
if(end == n -1 ) after = 0;
else after = list[end+1];
chd[pre] = after;
chd[e] = chd[pos];
chd[pos] = b;
}
bool dfs(int d){
int r = right();
if(r==n) {return true;}
if(n-r > 3*(maxd - d)) return false;
if(d>=maxd) return false;
int cpy[10];
memcpy(cpy, chd, sizeof chd);
for(int i = 0; i < n;i++){
for(int j = i; j < n;j++){
for(int pos = 0; pos<=n; pos++){
if(pos>=i&&pos<=j)continue;
move(i,j,pos);
if(dfs(d + 1)) return true;
memcpy(chd, cpy,sizeof cpy);
}
}
}
return false;
}
int main(){
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
int kase = 0;
while(cin>>n && n){
kase++;
int cur = 0;
memset(chd, 0, sizeof chd);
for(int i = 0; i < n; i++){
int c = cur;
cin>>cur;
chd[c] = cur;
}
ans = 0;
for(int i = 0;i < n;i++ ){
maxd = i;
if(dfs(0)) {ans = i; break;}
}
cout<<"Case "<<kase<<": "<<ans<<endl;
}
}