# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
563541 | Dymo | Swap (BOI16_swap) | C++14 | 0 ms | 340 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;
#define ll long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn=2e5+10;
const ll mod=1e9+7 ;
const ll base=2e9;
/// i believe myself
/// goal 2/7
ll a[maxn];
ll b[maxn];
ll valnw[maxn];
ll pos[maxn];
bool dd[maxn];
ll par[maxn];
ll valp[maxn];
void dosth(ll i,ll j)
{
swap(a[i],a[j]);
swap(pos[a[i]],pos[a[j]]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("t.inp","r"))
{
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
}
ll n;
cin>> n;
for (int i=1;i<=n;i++)
{
cin>> a[i];
pos[a[i]]=i;
// keep this value
}
for (int i=1;i<=n;i++)
{
pll val=make_pair(base,-1);
if (i*2<=n) val=min(val,make_pair(a[i*2],(ll)i*2));
if (i*2+1<=n) val=min(val,make_pair(a[i*2+1],(ll)i*2+1));
ll nw=i/2;
pll val1=make_pair(a[i],i/2);
ll pospar=-1;
while (nw)
{
if (!dd[nw]) val1=min(val1,make_pair(valp[nw],(ll)nw));
if (a[i]==valp[nw])
{
pospar=nw;
}
nw/=2;
}
if (val1.ff==a[i]) val1.ss=max(val1.ss,pospar);
if (val.ff>val1.ff)
{
dosth(i,pos[val1.ff]);
ll nw=i/2;
while (nw!=val1.ss)
{
dd[nw]=1;
}
dd[nw]=1;
}
else
{
if (val.ss==i*2)
{
dd[i]=1;
dosth(i,i*2);
}
else
{
valp[i]=min(a[i*2],a[i]);
dosth(i,i*2+1);
}
}
}
for (int i=1;i<=n;i++)
{
cout <<a[i]<<" ";
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |