# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
348140 | mariowong | 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>
#define x first
#define y second
#define pii pair<int,int>
#define ll long long
#define pll pair<ll,ll>
#define pbb pair<bool,bool>
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define xmod (ll)(1e9+7)
#define hmod 1286031825167LL
using namespace std;
int ans[5005],dif1[5005],dif2[5005],b[5005],ora[5005],mx,mn,n,pos1,posn;
pair <int,int> a[5005];
bool vis[5005];
int query(int a1,int a2){
mx=0; mn=n+1;
for (int i=a1;i<=a2;i++){
mx=max(mx,ora[i]);
mn=min(mn,ora[i]);
}
return mx-mn;
}
void answer(int a1,int a2){
ans[a1]=a2;
}
void solve(int N) {
for (int i=2;i<=N;i++){
dif1[i]=query(i-1,i);
}
for (int i=3;i<=N;i++){
dif2[i]=query(i-2,i);
}
a[2].first=0;
a[2].second=1;
for (int i=3;i<=N;i++){
if (dif1[i]+dif1[i-1] == dif2[i])
a[i]=a[i-1];
else
{
a[i].first=a[i-1].second;
a[i].second=a[i-1].first;
}
}
for (int i=1;i<=N;i++){
b[1]=i;
for (int j=1;j<=N;j++){
vis[j]=false;
}
for (int j=2;j<=N;j++){
if (a[j].first == 1)
b[j]=b[j-1]+dif1[j];
else
b[j]=b[j-1]-dif1[j];
}
for (int j=1;j<=N;j++){
if (b[j] == 1)
pos1=j;
if (b[j] == n)
posn=j;
if (b[j] < 1 || b[j] > N)
break;
if (vis[b[j]])
break;
vis[b[j]]=true;
if (j == N && pos1 < posn)
goto out;
}
for (int j=1;j<=N;j++){
vis[j]=false;
}
b[1]=i;
for (int j=2;j<=N;j++){
if (a[j].second == 1)
b[j]=b[j-1]+dif1[j];
else
b[j]=b[j-1]-dif1[j];
}
for (int j=1;j<=N;j++){
if (b[j] == 1)
pos1=j;
if (b[j] == n)
posn=j;
if (b[j] < 1 || b[j] > N)
break;
if (vis[b[j]])
break;
vis[b[j]]=true;
if (j == N && pos1 < posn)
goto out;
}
}
out:;
for(int i = 1; i <= N; i++) {
answer(i, b[i]);
}
}
int main(){
ios::sync_with_stdio(false);
cin >> n;
for (int i=1;i<=n;i++){
cin >> ora[i];
}
solve(n);
for (int i=1;i<=n;i++){
cout << ans[i] << " ";
}
cout << "\n";
return 0;
}