Submission #563687

# Submission time Handle Problem Language Result Execution time Memory
563687 2022-05-18T01:18:48 Z Dymo Swap (BOI16_swap) C++14
21 / 100
1 ms 340 KB
#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=4e5+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];
ll n;
pll par[maxn];
ll valpre[maxn];
ll st[maxn];

void dfs(ll u,ll x)
{
    a[u]=b[u];
    if (x)
        par[u]=make_pair(b[u],u);
    if (u*2<=n)
        dfs(u*2,1);
    if (u*2+1<=n)
        dfs(u*2+1,1);
}
pll pt(pll p,ll u)
{
    if (p.ss==u)
        return make_pair(base,-1);
    else
        return p;
}
void dfs1(ll u,ll mx)
{
    if (u>mx)
        return ;
    if (st[u]==0)
    {

    }
    else if (st[u]==1)
    {
        swap(a[u],a[u*2]);
        if (par[u].ss!=u)
            par[u*2]=par[u];
    }
    else if (st[u]==2)
    {
        swap(a[u],a[u*2+1]);
        ll h=min(a[u*2],a[u*2+1]);
        par[u*2]=min(make_pair(h,u),pt(par[u],u));
        par[u*2+1]=min(make_pair(h,u),pt(par[u],u));
    }
    else
    {
        swap(a[u],a[u*2+1]);
        if (st[u]==3)
            swap(a[u*2],a[u*2+1]);
        par[u*2]=min(pt(par[u],u),make_pair(a[u*2],u*2));
        par[u*2+1]=min(pt(par[u],u),make_pair(a[u*2+1],u*2+1));
    }
    dfs1(u*2,mx);
    dfs1(u*2+1,mx);
}
pll vals[maxn];

void dosth(ll x,ll y)
{
    swap(a[x],a[y]);
    swap(pos[a[x]],pos[a[y]]);
}
void update(ll x,ll y,ll p)
{
    swap(vals[p].ff,vals[p].ss);
    dosth(x,y);
    ll nw=x;
    do
    {
        nw/=2;
        if (nw==p)
            break;
        if (vals[nw].ff==vals[p].ss)
        {
            vals[nw].ff=vals[p].ff;
        }
        else
        {
            vals[nw].ss=vals[p].ff;
        }

    }
    while (nw!=p);
    nw=y;
    do
    {
        nw/=2;
        if (nw==p)
            break;
        if (vals[nw].ff==vals[p].ff)
        {
            vals[nw].ff=vals[p].ss;
        }
        else
        {
            vals[nw].ss=vals[p].ss;
        }
    }
    while (nw!=p);

}
bool dd[maxn];

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);
    }
    cin>> n;
    for (int i=1; i<=n; i++)
    {
        cin>> a[i];
        b[i]=a[i];
        pos[a[i]]=i;
        par[i]=make_pair(a[i],i);
        // keep this value
    }
    for (ll 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);
        ll pre=i;
        vector<ll> vtdd;

        while (nw)
        {
            if (st[nw]==2)
            {
                ll valnw=base;
                if (!dd[vals[nw].ff]&&!dd[vals[nw].ss]) valnw=min(valnw,min(vals[nw].ff,vals[nw].ss));
                val1=min(val1,make_pair(valnw,nw));
            }
            if (st[nw]==1&&nw*2!=pre)
                break;
            if (st[nw]>=3)
            {
                if (pre%2==0)
                {
                    dd[vals[nw].ss]=1;
                    vtdd.pb(vals[nw].ss);
                }
                else
                {
                    dd[vals[nw].ff]=1;
                    vtdd.pb(vals[nw].ff);
                }
            }
            pre=nw;
            nw/=2;
        }
        for (auto to:vtdd)
        {
            dd[to]=false;
      //      if (i==10) cout <<to<<" cdm"<<endl;
        }


        if (val.ff>val1.ff)
        {
            st[i]=0;
            if (val1.ss!=i)
            {
                ll nw=i;
                vector<ll> vt;
                vt.pb(nw);

                while (1)
                {
                    ll h=nw/2;
                    vt.pb(h);
                    if (h==val1.ss)
                        break;
                    else
                        nw=h;
                }



                reverse(vt.begin(),vt.end());
                ll t=(vals[val1.ss].ss==val1.ff);
                ll t1=(vt[1]==(val1.ss*2+1));
                ll p=val1.ss;
                vector<ll> vt1;
                if ((t^t1)==0)
                    st[val1.ss]=4;
                else
                {
                    // update(pos[vals[val1.ss].ff],pos[vals[val1.ss].ss],p);
                    st[val1.ss]=3;
                    vt1.pb(val1.ss);
                }


                for (int i=1; i+1<vt.size(); i++)
                {
                    ll x=vt[i];
                    if (st[x]==2)
                    {

                        st[x]=(3+(vt[i+1]%2));
                        if (st[x]==3)
                        {
                            vt1.pb(x);
                            //  update(pos[vals[x].ff],pos[vals[x].ss],p);
                        }
                    }
                }



                /*for (auto to:vt1)
                {
                    cout <<to<<" chk1"<<endl;
                }*/

             //   cout <<vt1.size()<<" "<<vt1[0]<<" "<<pos[vals[vt1[0]].ff]<<" "<<pos[vals[vt1[0]].ss]<<endl;
              //  reverse(vt1.begin(),vt1.end());
               // cout <<i<<" "<<vt1.size()<<" wtf"<<endl;
                for (auto x:vt1)
                {
                //    cout <<x<<" "<<vals[x].ff<<" "<<vals[x].ss<<" chkwtf"<<endl;
                    update(pos[vals[x].ff],pos[vals[x].ss],x);
                 //   cout <<vals[5].ff<<" "<<vals[5].ss<<" "<<pos[10]<<" "<<pos[11]<<" "<<vals[2].ff<<" "<<vals[2].ss<<" chk3"<<endl;
                }

            }
        }
        else
        {
            if (val.ss==i*2)
            {
                st[i]=1;
                dosth(i,i*2);
            }
            else
            {
                st[i]=2;
                valpre[i]=a[i];
                dosth(i,i*2+1);
                vals[i]=make_pair(a[i*2],a[i*2+1]);
            }
        }
        /*if (i)
        {
            cout <<i<<" wtf"<<endl;
            cout <<val.ff<<" "<<val.ss<<" "<<val1.ff<<" "<<val1.ss<<" chk1"<<endl;
            for (int i=1; i<=n; i++)
            {
                cout <<a[i]<<" "<<vals[i].ff<<" "<<vals[i].ss<<" "<<st[i]<<endl;
            }
            cout <<endl;
            //  cout <<t<<" "<<t1<<" "<<val1.ss<<endl;
         //     if (i==9) return 0;
        }*/
        /*for (int i=1;i<=n;i++)
        {
            cout <<a[i]<<" ";
        }
        cout <<endl;*/
    }
    for (int i=1; i<=n; i++)
    {
        cout <<a[i]<<" ";
    }
}

Compilation message

swap.cpp: In function 'int main()':
swap.cpp:226:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  226 |                 for (int i=1; i+1<vt.size(); i++)
      |                               ~~~^~~~~~~~~~
swap.cpp:214:20: warning: unused variable 'p' [-Wunused-variable]
  214 |                 ll p=val1.ss;
      |                    ^
swap.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |         freopen("test.inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
swap.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |         freopen("test.out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Incorrect 1 ms 340 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Incorrect 1 ms 340 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Incorrect 1 ms 340 KB Output isn't correct
12 Halted 0 ms 0 KB -