Recent actions

I don't invoke your solution but seems you are not change vist array inside your loop and it may cause infinite recursion!

i am adding each element at most once in the queue , it means at max its size will be 1e6 . then why it is throwing MEMORY_LIMIT_EXCEEDED

I think it's because of q size.

Can any one please help why I am getting MEMORY_LIMIT_EXCEEDED for C. Trapped in the Witch's Labyrinth my submission link -> submission

ig this is the easy and basic implementation for D question if you found the editorial of D difficult then you can check this

bool check(set<int> &s1,set<int> &s2){
    return *s1.rbegin()<*s2.begin();
}

void solve(){
    int n;cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    set<int> z,o,t;
    for(int i=0;i<n;i++){
        if(a[i]==0)z.insert(i);
        else if(a[i]==1)o.insert(i);
        else t.insert(i);
    }

    vector<pair<int,int>> ans;
    while(1){
        if(o.size()==0)break;
        if((z.size()+o.size()==0) || (z.size()+t.size()==0) || (o.size()+t.size()==0))break;


        if(z.size()==0){
            if(check(o,t))break;
            else{
                auto it1=*o.rbegin();
                auto it2=*t.begin();

                swap(a[it1],a[it2]);
                ans.pb({it2,it1});

                o.erase(it1);
                o.insert(it2);
                t.erase(it2);
                t.insert(it1);

            }

        }
        else if(t.size()==0){
            if(check(z,o))break;
            else{
                auto it1=*z.rbegin();
                auto it2=*o.begin();

                swap(a[it1],a[it2]);
                ans.pb({it2,it1});

                z.erase(it1);
                z.insert(it2);
                o.erase(it2);
                o.insert(it1);

            }

        }else{
            if(check(z,o)){
                if(check(o,t))break;
                else{
                    auto it1=*o.rbegin();
                    auto it2=*t.begin();

                    swap(a[it1],a[it2]);
                    ans.pb({it2,it1});

                    o.erase(it1);
                    o.insert(it2);
                    t.erase(it2);
                    t.insert(it1);
                }
            }
            else{
                auto it1=*z.rbegin();
                auto it2=*o.begin();

                swap(a[it1],a[it2]);
                ans.pb({it2,it1});

                z.erase(it1);
                z.insert(it2);
                o.erase(it2);
                o.insert(it1);

            }

        }
    }

    cout<<ans.size()<<"\n";
    for(auto x : ans)cout<<x.first+1<<" "<<x.second+1<<"\n";
}

Thank you....

Created or updated the text
Created or updated the text