This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "xylophone.h"
#include "bits/stdc++.h"
using namespace std;
int q[5005][5005], a[5005];
bool check_permutation(int n) {
bool b[n+1], pos[n+1];
memset(b, 0, sizeof(b));
for (int i = 1; i <= n; ++i) if (a[i] <= n) b[a[i]] = 1;
for (int i = 1; i <= n; ++i) if (!b[i]) return false;
int pos1, pos2;
for (int i = 1; i <= n; ++i) {
if (a[i] == 1) pos1 = i;
if (a[i] == n) pos2 = i;
}
if (pos1 > pos2) return false;
for (int i = 1; i < n; ++i) if (abs(a[i+1] - a[i]) != q[i][i+1]) return false;
for (int i = 1; i < n-1; ++i)
if (max(a[i], max(a[i+1], a[i+2])) - min(a[i], min(a[i+1], a[i+2])) != q[i][i+2]) return false;
return true;
}
void build_permutation(int n, int x, int y) {
a[1] = x;
a[2] = y;
for (int i = 3; i <= n; ++i) {
if (q[i-2][i-1] + q[i-1][i] == q[i-2][i]) {
if (a[i-2] > a[i-1]) a[i] = a[i-1] - q[i-1][i];
else a[i] = a[i-1] + q[i-1][i];
} else {
if (a[i-2] > a[i-1]) a[i] = a[i-1] + q[i-1][i];
else a[i] = a[i-1] - q[i-1][i];
}
}
int mi = *min_element(a+1, a+n+1) - 1;
for (int i = 1; i <= n; ++i) a[i] -= mi;
}
void solve(int n) {
for (int i = 1; i <= n-1; ++i) q[i][i+1] = q[i+1][i] = query(i, i+1);
for (int i = 1; i <= n-2; ++i) q[i][i+2] = q[i+2][i] = query(i, i+2);
for (int i = 1; i < n; ++i) {
build_permutation(n, 10000, 10000 + i);
if (check_permutation(n)) {
for (int i = 1; i <= n; ++i) answer(i, a[i]);
return;
}
}
for (int i = 1; i < n; ++i) {
build_permutation(n, 10000 + i, 10000);
if (check_permutation(n)) {
for (int i = 1; i <= n; ++i) answer(i, a[i]);
return;
}
}
}
Compilation message (stderr)
xylophone.cpp: In function 'bool check_permutation(int)':
xylophone.cpp:6:18: warning: unused variable 'pos' [-Wunused-variable]
6 | bool b[n+1], pos[n+1];
| ^~~
xylophone.cpp:15:5: warning: 'pos1' may be used uninitialized in this function [-Wmaybe-uninitialized]
15 | if (pos1 > pos2) return false;
| ^~
xylophone.cpp:15:5: warning: 'pos2' may be used uninitialized in this function [-Wmaybe-uninitialized]
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |