1846F - Rudolph and Mimic - CodeForces Solution


constructive algorithms greedy implementation interactive

Please click on ads to support us..

C++ Code:

#include<iostream>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        bool is_eq;
        // 1st
        int arr[n];
        int i;
        for(i=0;i<n;i++)
        {
            cin>>arr[i];
        }

        int count[9];
        for(i=0;i<9;i++)
        {
            count[i] = 0;
        }

        for(i=0;i<n;i++)
        {
            int ix = arr[i] - 1;
            count[ix]++;
        }
        cout<<"- "<<0<<endl;
        fflush(stdout); cout.flush();

        // 2nd and third
        int count_new[9];
REDO:   for(i=0;i<n;i++)
        {
            cin>>arr[i];
        }

        
        for(i=0;i<9;i++)
        {
            count_new[i] = 0;
        }

        for(i=0;i<n;i++)
        {
            int ix = arr[i] - 1;
            count_new[ix]++;
        }
        
        is_eq = true;
        for(i=0;i<9;i++)
        {
            if(count[i]!=count_new[i])
            {
                is_eq = false;
            }
        }

        if(is_eq)
        {
            cout<<"- "<<0<<endl;
            fflush(stdout); cout.flush();
            goto REDO;  
        }

        // They are uneq

        int his_ix;
        for(i=0;i<9;i++)
        {
            if(count_new[i]>count[i])
            {
                his_ix = i;
            }
        }

        int remove = 0;
        for(i=0;i<n;i++)
        {
            int x = arr[i] - 1;
            if(x!=his_ix)
            {
                remove++;
            }
        }
        // cout<<"His Ix is"<<his_ix<<endl;
        // for(i=0;i<9;i++)
        // {
        //     cout<<count[i]<<" ";
        // }
        // cout<<endl;
        // for(i=0;i<9;i++)
        // {
        //     cout<<count_new[i]<<" ";
        // }
        // cout<<endl;
        cout<<"- "<<remove<<" ";
        for(i=0;i<n;i++)
        {
            int x = arr[i] - 1;
            if(x!=his_ix)
            {
                cout<<i+1<<" ";
                count_new[x]--;
            }
            
        }
        cout<<endl;
        n-=remove;
        // cout<<"N is upd to "<<n<<endl;
        fflush(stdout); cout.flush();
        

        for(i=0;i<9;i++)
        {
            count[i] = count_new[i];            
        }

        // New lap
        // cout<<"New Lap"<<endl;
REDO1:  for(i=0;i<n;i++)
        {
            // cout<<"Hi5";
            cin>>arr[i];
        }

        for(i=0;i<9;i++)
        {
            // cout<<"Hi4";
            count_new[i] = 0;
        }

        for(i=0;i<n;i++)
        {
            // cout<<"Hi3";
            int ix = arr[i] - 1;
            count_new[ix]++;
        }

        is_eq = true;
        for(i=0;i<9;i++)
        {
            // cout<<"Hi2";
            if(count[i]!=count_new[i])
            {
                is_eq = false;
            }
        }

        if(is_eq)
        {
            // cout<<"Hi1";
            cout<<"- "<<0<<endl;
            fflush(stdout); cout.flush();
            goto REDO1;
        }

        // Now un

        // for(i=0;i<n;i++)
        // {
        //     cin>>arr[i];
        // }
        his_ix++;
        for(i=0;i<n;i++)
        {
            if(arr[i]!=his_ix)
            {
                cout<<"! "<<i+1<<endl;
                fflush(stdout); cout.flush();
            }
        }
    }
}


Comments

Submit
0 Comments
More Questions

e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees