-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQ10410.cpp
55 lines (48 loc) · 995 Bytes
/
Q10410.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
#include <stdio.h>
#include <string.h>
int n,f[1010],dfs[1010],bfs[1010],posd[1010];
void find(int now,int p) {
int b,d,i,min=n+1,minptr=0;
i = posd[bfs[p]];
for(b=now;b>0;b--) {
d = posd[bfs[b]];
if(d < i && i-d < min) {
min = i-d;
minptr = b;
}
}
f[p] = bfs[minptr];
}
void go(int now) {
if(now > n) return;
int cnt,i;
for(cnt=now+2;cnt<=n && bfs[cnt]>bfs[cnt-1];cnt++); //§ä¤W¤É§Ç¦C
for(i=now+2;i<cnt;i++)
if(posd[bfs[i]] < posd[bfs[i-1]])
break;
cnt = i;
for(i=now+1;i<cnt;i++)
if(f[i] == 0)
find(now,i); //©w¦ìª¨ª¨
go(cnt-1);
}
int main() {
int i,j;
while(scanf("%d",&n) != EOF) {
memset(f,0,sizeof(f));
for(i=1;i<=n;i++)
scanf("%d",bfs+i);
for(i=1;i<=n;i++)
scanf("%d",dfs+i);
for(i=1;i<=n;i++)
posd[dfs[i]] = i;
go(1);
for(i=1;i<=n;i++,puts("")) {
printf("%d:",i);
for(j=1;j<=n;j++)
if(f[j] == i)
printf(" %d",bfs[j]);
}
}
return 0;
}