#include <bits/stdc++.h>
#ifndef LOCAL
#include <xylophone.h>
#endif // LOCAL
using namespace std;
const int MAX = 5e3 + 5;
#ifdef LOCAL
int A[MAX];
int query(int s, int t){
return *max_element(A + s, A + t + 1) - *min_element(A + s, A + t + 1);
}
void answer(int i, int a){
// cout << i << " : " << a << '\n';
if(A[i] != a){
cout << "WRONG ANSWER\n";
exit(0);
}
}
#endif
int memo[MAX][MAX];
int ask(int l, int r){
assert(l <= r);
if(l == r) return 0;
if(memo[l][r] != 0) return memo[l][r];
return memo[l][r] = query(l, r);
}
void solve(int N){
memset(memo, 0, sizeof(memo));
if(N == 1){
answer(1, 1);
answer(2, 2);
return;
}
int l = 1, r = N, R = -1;
while(l <= r){
int mid = l + r >> 1;
if(ask(1, mid) == N - 1) R = mid, r = mid - 1;
else l = mid + 1;
}
l = 1, r = R - 1;
int L = 1;
while(l <= r){
int mid = l + r >> 1;
if(ask(mid, R) == N - 1) L = mid, l = mid + 1;
else r = mid - 1;
}
//find L, R that A[L] = 1, A[R] = N
vector<int> a(N + 1);
a[L] = 1;
a[R] = N;
if(L > 1) a[L - 1] = 1 + ask(L - 1, L);
if(R < N) a[R + 1] = N - ask(R, R + 1);
vector<bool> used(N + 1);
used[1] = used[N] = true;
auto casework = [&](int x, int y, int nextTwo, int nextThree){
int case1 = x - nextTwo;
if(max({case1, x, y}) - min({case1, x, y}) == nextThree) return case1;
int case2 = x + nextTwo;
if(max({case2, x, y}) - min({case2, x, y}) == nextThree) return case2;
cout << x << ' ' << y << ' ' << nextTwo << ' ' << nextThree << '\n';
assert(false);
};
for(int i = L - 2; i >= 1; --i){
int delta = ask(i, i + 1);
if(a[i + 1] - delta < 0 || used[a[i + 1] - delta]) a[i] = a[i + 1] + delta;
else if(a[i + 1] + delta > N || used[a[i + 1] + delta]) a[i] = a[i + 1] - delta;
else a[i] = casework(a[i + 1], a[i + 2], delta, ask(i, i + 2));
used[a[i]] = true;
}
for(int i = R + 2; i <= N; ++i){
int delta = ask(i - 1, i);
if(a[i - 1] - delta < 0 || used[a[i - 1] - delta]) a[i] = a[i - 1] + delta;
else if(a[i - 1] + delta > N || used[a[i - 1] + delta]) a[i] = a[i - 1] - delta;
else a[i] = casework(a[i - 1], a[i - 2], delta, ask(i - 2, i));
used[a[i]] = true;
}
if(L + 1 < R){
a[L + 1] = 1 + ask(L, L + 1);
for(int i = L + 2; i < R; ++i){
int delta = ask(i - 1, i);
if(a[i - 1] - delta < 0 || used[a[i - 1] - delta]) a[i] = a[i - 1] + delta;
else if(a[i - 1] + delta > N || used[a[i - 1] + delta]) a[i] = a[i - 1] - delta;
else a[i] = casework(a[i - 1], a[i - 2], delta, ask(i - 2, i));
used[a[i]] = true;
}
}
for(int i = 1; i <= N; ++i){
answer(i, a[i]);
}
}
#ifdef LOCAL
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int random(int l, int r){ return uniform_int_distribution<int>(l, r)(rng); }
int main(){
srand(time(nullptr));
for(int itest = 1; itest <= 300; ++itest){
int N = random(2, 30);
iota(A + 1, A + 1 + N, 1);
shuffle(A + 1, A + 1 + N, rng);
// int N = 6;
// vector<int> vec = {1, 6, 5, 3, 2, 4};
// copy(vec.begin(), vec.end(), A + 1);
int pos1 = -1, posN = -1;
for(int i = 1; i <= N; ++i){
if(A[i] == 1) pos1 = i;
if(A[i] == N) posN = i;
}
if(pos1 > posN){
swap(pos1, posN);
swap(A[pos1], A[posN]);
}
cout << "The permutation length N : ";
for(int i = 1; i <= N; ++i) cout << A[i] << ' '; cout << '\n';
solve(N);
cout << "Passed\n";
}
return 0;
}
#endif // LOCAL
Compilation message
xylophone.cpp: In function 'void solve(int)':
xylophone.cpp:44:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int mid = l + r >> 1;
| ~~^~~
xylophone.cpp:52:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
98384 KB |
Output is correct |
2 |
Correct |
13 ms |
98384 KB |
Output is correct |
3 |
Correct |
13 ms |
98384 KB |
Output is correct |
4 |
Correct |
15 ms |
98384 KB |
Output is correct |
5 |
Correct |
14 ms |
98384 KB |
Output is correct |
6 |
Correct |
15 ms |
98384 KB |
Output is correct |
7 |
Correct |
16 ms |
98280 KB |
Output is correct |
8 |
Correct |
14 ms |
98384 KB |
Output is correct |
9 |
Correct |
15 ms |
98288 KB |
Output is correct |
10 |
Correct |
14 ms |
98552 KB |
Output is correct |
11 |
Correct |
15 ms |
98384 KB |
Output is correct |
12 |
Correct |
15 ms |
98384 KB |
Output is correct |
13 |
Correct |
15 ms |
98384 KB |
Output is correct |
14 |
Correct |
14 ms |
98384 KB |
Output is correct |
15 |
Correct |
14 ms |
98224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
98384 KB |
Output is correct |
2 |
Correct |
13 ms |
98384 KB |
Output is correct |
3 |
Correct |
13 ms |
98384 KB |
Output is correct |
4 |
Correct |
15 ms |
98384 KB |
Output is correct |
5 |
Correct |
14 ms |
98384 KB |
Output is correct |
6 |
Correct |
15 ms |
98384 KB |
Output is correct |
7 |
Correct |
16 ms |
98280 KB |
Output is correct |
8 |
Correct |
14 ms |
98384 KB |
Output is correct |
9 |
Correct |
15 ms |
98288 KB |
Output is correct |
10 |
Correct |
14 ms |
98552 KB |
Output is correct |
11 |
Correct |
15 ms |
98384 KB |
Output is correct |
12 |
Correct |
15 ms |
98384 KB |
Output is correct |
13 |
Correct |
15 ms |
98384 KB |
Output is correct |
14 |
Correct |
14 ms |
98384 KB |
Output is correct |
15 |
Correct |
14 ms |
98224 KB |
Output is correct |
16 |
Correct |
15 ms |
98384 KB |
Output is correct |
17 |
Correct |
17 ms |
98408 KB |
Output is correct |
18 |
Correct |
19 ms |
98384 KB |
Output is correct |
19 |
Correct |
21 ms |
98384 KB |
Output is correct |
20 |
Correct |
21 ms |
98296 KB |
Output is correct |
21 |
Correct |
21 ms |
98384 KB |
Output is correct |
22 |
Correct |
23 ms |
98232 KB |
Output is correct |
23 |
Correct |
18 ms |
98384 KB |
Output is correct |
24 |
Correct |
18 ms |
98396 KB |
Output is correct |
25 |
Correct |
19 ms |
98384 KB |
Output is correct |
26 |
Correct |
21 ms |
98384 KB |
Output is correct |
27 |
Correct |
20 ms |
98384 KB |
Output is correct |
28 |
Correct |
21 ms |
98384 KB |
Output is correct |
29 |
Correct |
20 ms |
98396 KB |
Output is correct |
30 |
Correct |
19 ms |
98384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
98384 KB |
Output is correct |
2 |
Correct |
13 ms |
98384 KB |
Output is correct |
3 |
Correct |
13 ms |
98384 KB |
Output is correct |
4 |
Correct |
15 ms |
98384 KB |
Output is correct |
5 |
Correct |
14 ms |
98384 KB |
Output is correct |
6 |
Correct |
15 ms |
98384 KB |
Output is correct |
7 |
Correct |
16 ms |
98280 KB |
Output is correct |
8 |
Correct |
14 ms |
98384 KB |
Output is correct |
9 |
Correct |
15 ms |
98288 KB |
Output is correct |
10 |
Correct |
14 ms |
98552 KB |
Output is correct |
11 |
Correct |
15 ms |
98384 KB |
Output is correct |
12 |
Correct |
15 ms |
98384 KB |
Output is correct |
13 |
Correct |
15 ms |
98384 KB |
Output is correct |
14 |
Correct |
14 ms |
98384 KB |
Output is correct |
15 |
Correct |
14 ms |
98224 KB |
Output is correct |
16 |
Correct |
15 ms |
98384 KB |
Output is correct |
17 |
Correct |
17 ms |
98408 KB |
Output is correct |
18 |
Correct |
19 ms |
98384 KB |
Output is correct |
19 |
Correct |
21 ms |
98384 KB |
Output is correct |
20 |
Correct |
21 ms |
98296 KB |
Output is correct |
21 |
Correct |
21 ms |
98384 KB |
Output is correct |
22 |
Correct |
23 ms |
98232 KB |
Output is correct |
23 |
Correct |
18 ms |
98384 KB |
Output is correct |
24 |
Correct |
18 ms |
98396 KB |
Output is correct |
25 |
Correct |
19 ms |
98384 KB |
Output is correct |
26 |
Correct |
21 ms |
98384 KB |
Output is correct |
27 |
Correct |
20 ms |
98384 KB |
Output is correct |
28 |
Correct |
21 ms |
98384 KB |
Output is correct |
29 |
Correct |
20 ms |
98396 KB |
Output is correct |
30 |
Correct |
19 ms |
98384 KB |
Output is correct |
31 |
Correct |
36 ms |
98384 KB |
Output is correct |
32 |
Correct |
38 ms |
98384 KB |
Output is correct |
33 |
Correct |
48 ms |
98384 KB |
Output is correct |
34 |
Correct |
61 ms |
98384 KB |
Output is correct |
35 |
Correct |
60 ms |
98304 KB |
Output is correct |
36 |
Correct |
53 ms |
98384 KB |
Output is correct |
37 |
Correct |
44 ms |
98384 KB |
Output is correct |
38 |
Correct |
49 ms |
98384 KB |
Output is correct |
39 |
Correct |
64 ms |
98488 KB |
Output is correct |
40 |
Correct |
67 ms |
98392 KB |
Output is correct |
41 |
Correct |
64 ms |
98384 KB |
Output is correct |
42 |
Correct |
53 ms |
98384 KB |
Output is correct |
43 |
Correct |
46 ms |
98632 KB |
Output is correct |
44 |
Correct |
51 ms |
98384 KB |
Output is correct |
45 |
Correct |
67 ms |
98384 KB |
Output is correct |
46 |
Correct |
64 ms |
98484 KB |
Output is correct |
47 |
Correct |
67 ms |
98488 KB |
Output is correct |
48 |
Correct |
67 ms |
98384 KB |
Output is correct |
49 |
Correct |
68 ms |
98384 KB |
Output is correct |
50 |
Correct |
62 ms |
98384 KB |
Output is correct |
51 |
Correct |
60 ms |
98384 KB |
Output is correct |
52 |
Correct |
68 ms |
98492 KB |
Output is correct |
53 |
Correct |
63 ms |
98384 KB |
Output is correct |
54 |
Correct |
62 ms |
98384 KB |
Output is correct |
55 |
Correct |
63 ms |
98384 KB |
Output is correct |
56 |
Correct |
61 ms |
98444 KB |
Output is correct |
57 |
Correct |
59 ms |
98384 KB |
Output is correct |
58 |
Correct |
51 ms |
98384 KB |
Output is correct |
59 |
Correct |
63 ms |
98384 KB |
Output is correct |
60 |
Correct |
70 ms |
98496 KB |
Output is correct |