1768C - Elemental Decompress - CodeForces Solution


constructive algorithms greedy implementation sortings *1300

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
void solve() 
{
   ll n; cin>>n;
   vector<ll> v(n),a(n,0),b(n,0),c(n,0),r(n,0);
   set<ll> s1,s2;
   for(ll i=0;i<n;i++)
    {
        cin>>v[i];
        if(v[i]>n)
        {
            cout<<"NO"<<endl;
            return ;
        }
        r[v[i]-1]++;
    }
    for(ll i=0;i<n;i++)
    {
        if(r[i]>2)
        {
            cout<<"NO"<<endl;
            return ;
        }
    }
   for(ll i=1;i<=n;i++)
   {
       s1.insert(i);
       s2.insert(i);
   }
   for(ll i=0;i<n;i++)
   {
       if(c[v[i]-1]==0)
       {
           a[i]=v[i];
           c[v[i]-1]=1;
           s1.erase(v[i]);
       }
       else
       {
           b[i]=v[i];
           s2.erase(v[i]);
       }
   }
   for(ll i=0;i<n;i++)
   {
       if(a[i]==0)
       {
           auto it=s1.upper_bound(b[i]);
           it--;
           if((*it)<=b[i])
           {
               a[i]=*it;
               s1.erase(a[i]);
           }
           else
           {
               cout<<"NO"<<endl;
               return;
           }
       }
   }
   for(ll i=0;i<n;i++)
   {
       if(b[i]==0)
       {
           auto it=s2.upper_bound(a[i]);
           it--;
           if((*it)<=a[i])
           {
               b[i]=*it;
               s2.erase(b[i]);
           }
           else
           {
               cout<<"NO"<<endl;
               return;
           }
       }
   }
   cout<<"YES"<<endl;
    for(auto x:a)
    cout<<x<<" ";
    cout<<endl;
    for(auto x:b)
    cout<<x<<" ";
    cout<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL) ;
    ll t=1; cin>>t;
    while(t--)
    solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

987A - Infinity Gauntlet
1676G - White-Black Balanced Subtrees
1716D - Chip Move
1352F - Binary String Reconstruction
1487B - Cat Cycle
1679C - Rooks Defenders
56A - Bar
1694B - Paranoid String
35A - Shell Game
1684A - Digit Minimization
43B - Letter
1017A - The Rank
1698B - Rising Sand
235A - LCM Challenge
1075B - Taxi drivers and Lyft
1562A - The Miracle and the Sleeper
1216A - Prefixes
1490C - Sum of Cubes
868A - Bark to Unlock
873B - Balanced Substring
1401D - Maximum Distributed Tree
1716C - Robot in a Hallway
1688B - Patchouli's Magical Talisman
99A - Help Far Away Kingdom
622B - The Time
1688C - Manipulating History
1169D - Good Triple
1675B - Make It Increasing
588A - Duff and Meat
1541B - Pleasant Pairs