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;
const int MAXN = 5069;
int ans[MAXN], vis[MAXN];
void solve(int N) {
int n = N;
int l = 1, r = n, mx = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (query(1, mid) == n - 1) {
mx = mid;
r = mid - 1;
}
else
l = mid + 1;
}
assert(mx != -1);
ans[mx] = n;
vis[n] = 1;
if (mx < n) {
int x = query(mx, mx + 1);
ans[mx + 1] = n - x;
vis[n - x] = 1;
}
if (mx > 1) {
int x = query(mx - 1, mx);
ans[mx - 1] = n - x;
vis[n - x] = 1;
}
for (int i = mx - 2;i >= 1;i--) {
int x = query(i, i + 1);
if (ans[i + 1] - x < 1 || vis[ans[i + 1] - x])
ans[i] = ans[i + 1] + x;
else if (ans[i + 1] + x > n || vis[ans[i + 1] + x])
ans[i] = ans[i + 1] - x;
else {
int y = query(i, i + 2);
if (y != x) {
if (ans[i + 2] > ans[i + 1])
ans[i] = ans[i + 1] + x;
else
ans[i] = ans[i + 1] - x;
}
else {
if (ans[i + 2] > ans[i + 1])
ans[i] = ans[i + 1] - x;
else
ans[i] = ans[i + 1] + x;
}
}
vis[ans[i]] = 1;
}
for (int i = mx + 2;i <= n;i++) {
int x = query(i - 1, i);
if (ans[i - 1] - x < 1 || vis[ans[i - 1] - x])
ans[i] = ans[i - 1] + x;
else if (ans[i - 1] + x > n || vis[ans[i - 1] + x])
ans[i] = ans[i - 1] - x;
else {
int y = query(i - 2, i);
if (y != x) {
if (ans[i - 2] > ans[i - 1])
ans[i] = ans[i - 1] + x;
else
ans[i] = ans[i - 1] - x;
}
else {
if (ans[i - 2] > ans[i - 1])
ans[i] = ans[i - 1] - x;
else
ans[i] = ans[i - 1] + x;
}
}
vis[ans[i]] = 1;
}
for(int i = 1; i <= n; i++) {
answer(i, ans[i]);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |