Submission #729234

# Submission time Handle Problem Language Result Execution time Memory
729234 2023-04-23T17:10:24 Z Rafi22 Swap (BOI16_swap) C++14
0 / 100
1 ms 340 KB
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=998244353;
int inf=1000000007;
ll infl=1000000000000000007;

const int N=200007;

int p[4*N];
int n;

int calc(int v,int x,int i)
{
    if(v>n) return inf;
    vector<int>V={x,p[2*v],p[2*v+1]};
    sort(all(V));
    if(x==V[0]) return i;
    else if(p[2*v]==V[0]) return calc(2*v,x,i+1);
    else return min(calc(2*v,x,i+1),calc(2*v+1,x,i+1));
}

void build(int v)
{
    if(v>n) return ;
    vector<int>V={p[v],p[2*v],p[2*v+1]};
    //cout<<v<<" "<<p[v]<<" "<<p[2*v]<<" "<<p[2*v+1]<<endl;
    sort(all(V));
    if(p[v]==V[0])
    {
     /*   if(p[2*v]!=inf&&p[2*v+1]!=inf)
        {
            if(calc(2*v,V[1],0)<=calc(2*v+1,V[2],0))
            {
                p[2*v]=V[1];
                p[2*v+1]=V[2];
            }
            else
            {
                p[2*v]=V[2];
                p[2*v+1]=V[1];
            }
        }*/
    }
    else if(p[2*v]==V[0]) swap(p[2*v],p[v]);
    else
    {
        swap(p[2*v+1],p[v]);
        if(p[2*v]!=inf&&p[2*v+1]!=inf)
        {
            if(calc(2*v,V[1],0)<=calc(2*v+1,V[2],0))
            {
                p[2*v]=V[1];
                p[2*v+1]=V[2];
            }
            else
            {
                p[2*v]=V[2];
                p[2*v+1]=V[1];
            }
        }
    }
    build(2*v);
    build(2*v+1);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>p[i];
    for(int i=n+1;i<=4*n;i++) p[i]=inf;
    build(1);
    for(int i=1;i<=n;i++) cout<<p[i]<<" ";


    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -