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 ans[5005], taken[5005];
void solve(int N) {
int x = 2,x1 = N - 1, prev= 0, ans1 = query(2, N), ans2 = query(1, N-1);
while(ans2 == N - 1 && ans1 == N - 1){
x++;
x1--;
ans1 = query(x, N);
ans2 = query(x1, N);
}
if(ans1 != N-1)ans[--x] = 1, taken[1] = true;
else ans[x1+1] = N, taken[N] = true, x = x1 + 1;
for(int i=x-1;i>=1;i--){
int y = query(i, i+1);
if(ans[i+1] -y < 1 || taken[ans[i+1] - y])ans[i] = ans[i+1] + y;
else if(ans[i+1] + y > N || taken[ans[i+1] + y])ans[i] = ans[i+1] - y;
else{
int y2 = query(i, i+2);
if(y2 != prev){
if(ans[i+1] + y == min(ans[i+1] ,ans[i+2]) + y2)ans[i] = ans[i+1] + y;
else ans[i] = ans[i+1] - y;
}
else{
if(ans[i+1] > ans[i+2])ans[i]=ans[i+1] -y;
else ans[i] = ans[i+1] + y;
}
}
taken[ans[i]] = true;
prev = y;
}
prev = 0;
for(int i=x+1;i<=N;i++){
int y = query(i-1, i);
if(ans[i-1] -y < 1 || taken[ans[i-1] - y])ans[i] = ans[i-1] + y;
else if(ans[i-1] + y > N || taken[ans[i-1] + y])ans[i] = ans[i-1] - y;
else{
int y2 = query(i-2, i);
if(y2 != prev){
if(ans[i-1] + y == min(ans[i-1], ans[i-2]) + y2)ans[i] = ans[i-1] + y;
else ans[i] = ans[i-1] - y;
}
else{
if(ans[i-1] > ans[i-2])ans[i]=ans[i-1] -y;
else ans[i] = ans[i-1] + y;
}
}
prev = y;
taken[ans[i]] = true;
}
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... |