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;
//#define int long long
const int mod=1e9+7;
void solve(int n){
int id1=0,l=1,r=n;
while(l<=r){
int mid=(l+r)/2;
int x=query(mid,n);
if (x==n-1) {
id1=mid;
l=mid+1;
}else{
r=mid-1;
}
}
int idn=0;
l=id1+1;
r=n;
while(l<=r){
int mid=(l+r)/2;
int x=query(id1,mid);
if (x==n-1) {
idn=mid;
r=mid-1;
}else{
l=mid+1;
}
}
vector<int> vis(n+1,0),ans(n+1,0);
vis[1]=1;
vis[n]=1;
ans[id1]=1;
ans[idn]=n;
for(int i=id1-1;i>=1;i--){
int q=query(i,i+1);
int a=ans[i+1]+q;
int b=ans[i+1]-q;
if(b<=0 || vis[b]==1){
ans[i]=a;
vis[a]=1;
}else if(a>n || vis[a]==1){
ans[i]=b;
vis[b]=1;
}else{
int x=query(i,i+2);
int mx=max(a,max(ans[i+1],ans[i+2]));
int mn=min(a,min(ans[i+1],ans[i+2]));
if(x==mx-mn){
ans[i]=a;
vis[a]=1;
}else{
ans[i]=b;
vis[b]=1;
}
}
}
for(int i=idn-1;i>id1;i--){
int q=query(i,i+1);
int a=ans[i+1]+q;
int b=ans[i+1]-q;
if(b<=0 || vis[b]==1){
ans[i]=a;
vis[a]=1;
}else if(a>n || vis[a]==1){
ans[i]=b;
vis[b]=1;
}else{
int x=query(i,i+2);
int mx=max(a,max(ans[i+1],ans[i+2]));
int mn=min(a,min(ans[i+1],ans[i+2]));
if(x==mx-mn){
ans[i]=a;
vis[a]=1;
}else{
ans[i]=b;
vis[b]=1;
}
}
}
for(int i=idn+1;i<=n;i++){
int q=query(i,i-1);
int a=ans[i-1]+q;
int b=ans[i-1]-q;
if(b<=0 || vis[b]==1){
ans[i]=a;
vis[a]=1;
}else if(a>n || vis[a]==1){
ans[i]=b;
vis[b]=1;
}else{
int x=query(i-2,i);
int mx=max(a,max(ans[i-1],ans[i-2]));
int mn=min(a,min(ans[i-1],ans[i-2]));
if(x==mx-mn){
ans[i]=a;
vis[a]=1;
}else{
ans[i]=b;
vis[b]=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... |