# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
590498 | nota | Xylophone (JOI18_xylophone) | C++14 | 0 ms | 0 KiB |
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<bits/stdc++.h>
using namespace std;
const int maxN = 5001;
int a[maxN];
int chk[maxN];
void solve(int n)
{
int l = 1, r = n;
while(l <= r)
{
int mid = (l+r)/2;
if(query(mid, n) == n-1) l = mid + 1;
else r = mid - 1;
}
answer(r, 1);
a[r] = 1;
chk[1] = true;
for(int i = r - 1; i >= 1; i--)
{
int que = query(i, i+1);
if(a[i+1] - que < 1)
{
a[i] = a[i+1] + que;
answer(i,a[i]);
}
else if(a[i+1] + que > n)
{
a[i] = a[i+1] - que;
answer(i,a[i]);
}
else
{
if(chk[a[i+1]-que])
{
a[i] = a[i+1] + que;
answer(i, a[i]);
}
else if(chk[a[i+1] + que])
{
a[i] = a[i+1] - que;
answer(i,a[i]);
}
else
{
int c = a[i+1] - que;
int maxx,minn;
maxx = max(max(c,a[i+1]),a[i+2]);
minn = min(min(c,a[i+1]),a[i+2]);
if(query(i,i+2) == maxx - minn)
{
a[i] = c;
answer(i,c);
}
else
{
a[i] = a[i+1] + que;
answer(i,a[i+1] + que);
}
}
}
chk[a[i]] = true;
}
for(int i = r + 1; i <= n; i++)
{
int que = query(i-1,i);
if(a[i-1] - que < 1)
{
a[i] = a[i-1] + que;
answer(i,a[i]);
}
else if(a[i-1] + que > n)
{
a[i] = a[i-1] - que;
answer(i,a[i]);
}
else
{
if(chk[a[i-1]-que])
{
a[i] = a[i-1] + que;
answer(i,a[i]);
}
else if(chk[a[i-1]+que])
{
a[i] = a[i-1] - que;
answer(i,a[i]);
}
else
{
int c = a[i-1] - que;
int maxx,minn;
maxx = max(max(c,a[i-1]),a[i-2]);
minn = min(min(c,a[i-1]),a[i-2]);
if(query(i-2,i) == maxx - minn)
{
a[i] = c;
answer(i,c);
}
else
{
a[i] = a[i-1] + que;
answer(i,a[i-1] + que);
}
}
}
chk[a[i]] = true;
}
}