# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
226193 | MKopchev | Swap (BOI16_swap) | C++14 | 697 ms | 3704 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 nmax=2e5+42;
int n,inp[nmax];
int would_go(int pos,int val)
{
if(2*pos>n)return pos;
if(val<inp[2*pos]&&val<inp[2*pos+1])return pos;
if(inp[2*pos]<val&&inp[2*pos]<inp[2*pos+1])return would_go(2*pos,val);
int x=val,y=inp[2*pos];
if(x>y)swap(x,y);
if(would_go(2*pos,x)<would_go(2*pos+1,x))
{
if(val==x)return would_go(2*pos,val);
return would_go(2*pos+1,val);
}
if(val==x)return would_go(2*pos+1,val);
return would_go(2*pos,val);
}
int main()
{
scanf("%i",&n);
for(int i=1;i<=n;i++)scanf("%i",&inp[i]);
inp[n+1]=n+1;
for(int i=1;2*i+1<=n;i++)
{
if(inp[i]<inp[2*i]&&inp[i]<inp[2*i+1])continue;
if(inp[2*i]<inp[i]&&inp[2*i]<inp[2*i+1]){swap(inp[i],inp[2*i]);continue;}
int x=inp[i],y=inp[2*i],small=inp[2*i+1];
if(x>y)swap(x,y);
if(would_go(2*i,x)<would_go(2*i+1,x)){inp[2*i]=x;inp[2*i+1]=y;}
else {inp[2*i]=y;inp[2*i+1]=x;}
inp[i]=small;
}
if(n%2==0&&inp[n/2]>inp[n])swap(inp[n/2],inp[n]);
for(int i=1;i<=n;i++)
{
printf("%i",inp[i]);
if(i==n)printf("\n");
else printf(" ");
}
return 0;
}
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... |