#include "xylophone.h"
#include <bits/stdc++.h>
using namespace std;
int p[5002],a[5002],mem[5002][5002];
int QQ(int l,int r) {
if(mem[l][r] != -1) {
return mem[l][r];
}
return mem[l][r] = query(l,r);
}
int n_;
bool bad(int x) {
return (x < 1 || x > n_ || p[x]);
}
void make(int v,int val) {
a[v] = val;
p[val] = v;
}
void solve(int n) {
n_ = n;
memset(mem,-1,sizeof(mem));
memset(p,0,sizeof(p));
memset(a,0,sizeof(a));
int nd = n - 1;
int l = 1,r = n;
while(r - l > 1) {
int mid = (l + r) >> 1;
if(QQ(1,mid) == nd) {
r = mid;
} else {
l = mid;
}
}
p[n] = r;
l = 1,r = p[n];
while(r - l > 1) {
int mid = (l + r) >> 1;
if(QQ(mid,p[n]) == nd) {
l = mid;
} else {
r = mid;
}
}
p[1] = l;
a[p[1]] = 1;
a[p[n]] = n;
int mx = 1;
if(p[1] - 1 >= 1) {
make(p[1] - 1,1 + QQ(p[1] - 1,p[1]));
}
for(int i = p[1] - 2;i >= 1;i--) {
int t2, t1, val;
int t = QQ(i,i + 1);
if(bad(a[i + 1] + t)) {
val = a[i + 1] - t;
} else if(bad(a[i + 1] - t)) {
val = a[i + 1] + t;
}
else {
t2 = QQ(i,i + 2);
if(a[i + 2] > a[i + 1]) {
if(t2 == a[i + 2] - (a[i + 1] - t)) {
val = a[i + 1] - t;
} else {
val = a[i + 1] + t;
}
} else {
if(t2 == a[i + 1] + t - a[i + 2]) {
val = a[i + 1] + t;
} else {
val = a[i + 1] - t;
}
}
}
p[val] = i;
a[i] = val;
}
mx = 1;
if(p[1] + 1 < p[n]) {
make(p[1] + 1,1 + QQ(p[1],p[1]+1));
}
for(int i = p[1] + 2;i < p[n];i++) {
int val;
int t2, t1;
int t = QQ(i - 1,i);
if(bad(a[i - 1] + t)) {
val = a[i - 1] - t;
} else if(bad(a[i - 1] - t)) {
val = a[i - 1] + t;
} else{
t2 = QQ(i - 2,i);
if(a[i - 2] > a[i - 1]) {
if(t2 == a[i - 2] - (a[i - 1] - t)) {
val = a[i - 1] - t;
} else {
val = a[i - 1] + t;
}
} else {
if(t2 == a[i - 1] + t - a[i - 2]) {
val = a[i - 1] + t;
} else {
val = a[i - 1] - t;
}
}
}
// cout << i << ' ' << val << '\n';
p[val] = i;
a[i] = val;
}
int mn = n;
if(p[n] + 1 <= n) {
make(p[n] + 1,n - QQ(p[n],p[n]+1));
}
for(int i = p[n] + 2;i <= n;i++) {
int val;
int t2, t1;
int t = QQ(i - 1,i);
if(bad(a[i - 1] + t)) {
val = a[i - 1] - t;
} else if(bad(a[i - 1] - t)) {
val = a[i - 1] + t;
} else{
t2 = QQ(i - 2,i);
if(a[i - 2] > a[i - 1]) {
if(t2 == a[i - 2] - (a[i - 1] - t)) {
val = a[i - 1] - t;
} else {
val = a[i - 1] + t;
}
} else {
if(t2 == a[i - 1] + t - a[i - 2]) {
val = a[i - 1] + t;
} else {
val = a[i - 1] - t;
}
}
}
p[val] = i;
a[i] = val;
}
for(int i = 1;i <= n;i++) {
answer(p[i],i);
}
}
Compilation message
xylophone.cpp: In function 'void solve(int)':
xylophone.cpp:52:11: warning: unused variable 't1' [-Wunused-variable]
52 | int t2, t1, val;
| ^~
xylophone.cpp:84:11: warning: unused variable 't1' [-Wunused-variable]
84 | int t2, t1;
| ^~
xylophone.cpp:116:11: warning: unused variable 't1' [-Wunused-variable]
116 | int t2, t1;
| ^~
xylophone.cpp:47:6: warning: variable 'mx' set but not used [-Wunused-but-set-variable]
47 | int mx = 1;
| ^~
xylophone.cpp:110:6: warning: unused variable 'mn' [-Wunused-variable]
110 | int mn = n;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
98128 KB |
Output is correct |
2 |
Correct |
25 ms |
98392 KB |
Output is correct |
3 |
Correct |
33 ms |
98380 KB |
Output is correct |
4 |
Correct |
25 ms |
98200 KB |
Output is correct |
5 |
Correct |
25 ms |
98128 KB |
Output is correct |
6 |
Correct |
25 ms |
98192 KB |
Output is correct |
7 |
Correct |
25 ms |
98392 KB |
Output is correct |
8 |
Correct |
27 ms |
98388 KB |
Output is correct |
9 |
Correct |
25 ms |
98392 KB |
Output is correct |
10 |
Correct |
25 ms |
98128 KB |
Output is correct |
11 |
Correct |
26 ms |
98392 KB |
Output is correct |
12 |
Correct |
25 ms |
98372 KB |
Output is correct |
13 |
Correct |
31 ms |
98392 KB |
Output is correct |
14 |
Correct |
26 ms |
98128 KB |
Output is correct |
15 |
Correct |
25 ms |
98392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
98128 KB |
Output is correct |
2 |
Correct |
25 ms |
98392 KB |
Output is correct |
3 |
Correct |
33 ms |
98380 KB |
Output is correct |
4 |
Correct |
25 ms |
98200 KB |
Output is correct |
5 |
Correct |
25 ms |
98128 KB |
Output is correct |
6 |
Correct |
25 ms |
98192 KB |
Output is correct |
7 |
Correct |
25 ms |
98392 KB |
Output is correct |
8 |
Correct |
27 ms |
98388 KB |
Output is correct |
9 |
Correct |
25 ms |
98392 KB |
Output is correct |
10 |
Correct |
25 ms |
98128 KB |
Output is correct |
11 |
Correct |
26 ms |
98392 KB |
Output is correct |
12 |
Correct |
25 ms |
98372 KB |
Output is correct |
13 |
Correct |
31 ms |
98392 KB |
Output is correct |
14 |
Correct |
26 ms |
98128 KB |
Output is correct |
15 |
Correct |
25 ms |
98392 KB |
Output is correct |
16 |
Correct |
27 ms |
98392 KB |
Output is correct |
17 |
Correct |
30 ms |
98152 KB |
Output is correct |
18 |
Correct |
30 ms |
98372 KB |
Output is correct |
19 |
Correct |
30 ms |
98640 KB |
Output is correct |
20 |
Correct |
35 ms |
98376 KB |
Output is correct |
21 |
Correct |
36 ms |
98244 KB |
Output is correct |
22 |
Correct |
34 ms |
98392 KB |
Output is correct |
23 |
Correct |
29 ms |
98324 KB |
Output is correct |
24 |
Correct |
32 ms |
98392 KB |
Output is correct |
25 |
Correct |
34 ms |
98648 KB |
Output is correct |
26 |
Correct |
33 ms |
98376 KB |
Output is correct |
27 |
Correct |
30 ms |
98392 KB |
Output is correct |
28 |
Correct |
30 ms |
98408 KB |
Output is correct |
29 |
Correct |
33 ms |
98640 KB |
Output is correct |
30 |
Correct |
32 ms |
98392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
98128 KB |
Output is correct |
2 |
Correct |
25 ms |
98392 KB |
Output is correct |
3 |
Correct |
33 ms |
98380 KB |
Output is correct |
4 |
Correct |
25 ms |
98200 KB |
Output is correct |
5 |
Correct |
25 ms |
98128 KB |
Output is correct |
6 |
Correct |
25 ms |
98192 KB |
Output is correct |
7 |
Correct |
25 ms |
98392 KB |
Output is correct |
8 |
Correct |
27 ms |
98388 KB |
Output is correct |
9 |
Correct |
25 ms |
98392 KB |
Output is correct |
10 |
Correct |
25 ms |
98128 KB |
Output is correct |
11 |
Correct |
26 ms |
98392 KB |
Output is correct |
12 |
Correct |
25 ms |
98372 KB |
Output is correct |
13 |
Correct |
31 ms |
98392 KB |
Output is correct |
14 |
Correct |
26 ms |
98128 KB |
Output is correct |
15 |
Correct |
25 ms |
98392 KB |
Output is correct |
16 |
Correct |
27 ms |
98392 KB |
Output is correct |
17 |
Correct |
30 ms |
98152 KB |
Output is correct |
18 |
Correct |
30 ms |
98372 KB |
Output is correct |
19 |
Correct |
30 ms |
98640 KB |
Output is correct |
20 |
Correct |
35 ms |
98376 KB |
Output is correct |
21 |
Correct |
36 ms |
98244 KB |
Output is correct |
22 |
Correct |
34 ms |
98392 KB |
Output is correct |
23 |
Correct |
29 ms |
98324 KB |
Output is correct |
24 |
Correct |
32 ms |
98392 KB |
Output is correct |
25 |
Correct |
34 ms |
98648 KB |
Output is correct |
26 |
Correct |
33 ms |
98376 KB |
Output is correct |
27 |
Correct |
30 ms |
98392 KB |
Output is correct |
28 |
Correct |
30 ms |
98408 KB |
Output is correct |
29 |
Correct |
33 ms |
98640 KB |
Output is correct |
30 |
Correct |
32 ms |
98392 KB |
Output is correct |
31 |
Correct |
47 ms |
98392 KB |
Output is correct |
32 |
Correct |
46 ms |
98384 KB |
Output is correct |
33 |
Correct |
54 ms |
98380 KB |
Output is correct |
34 |
Correct |
51 ms |
98388 KB |
Output is correct |
35 |
Correct |
76 ms |
98380 KB |
Output is correct |
36 |
Correct |
54 ms |
98188 KB |
Output is correct |
37 |
Correct |
60 ms |
98384 KB |
Output is correct |
38 |
Correct |
65 ms |
98376 KB |
Output is correct |
39 |
Correct |
66 ms |
98392 KB |
Output is correct |
40 |
Correct |
64 ms |
98384 KB |
Output is correct |
41 |
Correct |
63 ms |
98380 KB |
Output is correct |
42 |
Correct |
50 ms |
98380 KB |
Output is correct |
43 |
Correct |
67 ms |
98392 KB |
Output is correct |
44 |
Correct |
56 ms |
98128 KB |
Output is correct |
45 |
Correct |
62 ms |
98380 KB |
Output is correct |
46 |
Correct |
51 ms |
98380 KB |
Output is correct |
47 |
Correct |
62 ms |
98212 KB |
Output is correct |
48 |
Correct |
70 ms |
98376 KB |
Output is correct |
49 |
Correct |
63 ms |
98384 KB |
Output is correct |
50 |
Correct |
61 ms |
98380 KB |
Output is correct |
51 |
Correct |
57 ms |
98392 KB |
Output is correct |
52 |
Correct |
63 ms |
98384 KB |
Output is correct |
53 |
Correct |
60 ms |
98392 KB |
Output is correct |
54 |
Correct |
60 ms |
98392 KB |
Output is correct |
55 |
Correct |
60 ms |
98484 KB |
Output is correct |
56 |
Correct |
68 ms |
98184 KB |
Output is correct |
57 |
Correct |
62 ms |
98384 KB |
Output is correct |
58 |
Correct |
64 ms |
98380 KB |
Output is correct |
59 |
Correct |
58 ms |
98380 KB |
Output is correct |
60 |
Correct |
64 ms |
98392 KB |
Output is correct |