# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
302870 | iliccmarko | 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 "xylophone.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
using namespace std;
int used[5005];
int a[5005];
void solve(int n)
{
int l = 1;
int r = n;
int start;
while(l<=r)
{
int mid = (l+r)/2;
int s = query(mid, n);
if(s==n-1)
{
start = mid;
l = mid + 1;
}
else
{
r = mid - 1;
}
}
a[start] = 1;
used[1] = 1;
if(start-1)
{
int k = 1 + query(start-1, start);
a[start-1] = k;
used[k] = 1;
}
if(start+1<=n)
{
int k = 1 + query(start, start+1);
a[start+1] = k;
used[k] = 1;
}
for(int i = start-2;i;i--)
{
int s = query(i, i + 1);
int m1, m2;
m1 = a[i+1] + s;
m2 = a[i+1] - s;
if(m1>n)
{
used[m2] = 1;
a[i] = m2;
continue;
}
if(m2<=0)
{
used[m1] = 1;
a[i] = m2;
continue;
}
if(used[m1])
{
a[i] = m2;
used[m2] = 1;
continue;
}
if(used[m2])
{
a[i] = m1;
used[m1] = 1;
continue;
}
int ss = query(i, i + 2);
if(max(a[i+2], m1) - min(a[i+1], a[i+2]) == ss)
{
a[i] = m1;
used[m1] = 1;
}
else
{
a[i] = m2;
used[m2] = 1;
}
}
for(int i = start+2;i<=n;i++)
{
int s = guery(i-1, i);
int m1, m2;
m1 = a[i-1] + s;
m2 = a[i-1] - s;
if(m1>n)
{
used[m2] = 1;
a[i] = m2;
continue;
}
if(m2<=0)
{
used[m1] = 1;
a[i] = m2;
continue;
}
if(used[m1])
{
a[i] = m2;
used[m2] = 1;
continue;
}
if(used[m2])
{
a[i] = m1;
used[m1] = 1;
continue;
}
int ss = guery(i-2, i);
if(max(a[i-2], m1) - min(a[i-2], a[i-1]) == ss)
{
a[i] = m1;
used[m1] = 1;
}
else
{
a[i] = m2;
used[m2] = 1;
}
}
for(int i = 1;i<=n;i++)
{
answer(i, a[i]);
}
}