1786D - Letter Exchange - CodeForces Solution


brute force constructive algorithms graphs implementation

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
 
struct moves
{
    int i1, i2;
    char c1, c2;
};
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
 
    int tt; cin >> tt;
 
    while(tt--)
    {
        int n; cin >> n;
 
        vector<string>a(n);
        for(auto &i : a)cin >> i;
 
        set<int>adj[3][3];
        //adj[maxi][not_present] ;
        vector<moves>answer;
 
        string s = "win";
 
        for(int i = 0; i < n; i++)
        {
            int W = 0, I = 0, N = 0;
 
            for(auto &c : a[i])
            {
                if(c == 'w') W++;
                if(c == 'i') I++;
                if(c == 'n') N++;
            }
 
            if(W >= 2 && !I) adj[0][1].insert(i);
            if(W >= 2 && !N) adj[0][2].insert(i);
            if(I >= 2 && !W) adj[1][0].insert(i);
            if(I >= 2 && !N) adj[1][2].insert(i);
            if(N >= 2 && !W) adj[2][0].insert(i);
            if(N >= 2 && !I) adj[2][1].insert(i);
        }
 
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {     // 0,2==wwi
                  //2,0==nni
                    while(!adj[i][j].empty() && !adj[j][i].empty())
                    {
                        moves A; 
                        A.i1 = *adj[i][j].begin();
                        A.i2 = *adj[j][i].begin();
                        A.c1 = s[i]; A.c2 = s[j];
    
                        adj[i][j].erase(A.i1);
                        adj[j][i].erase(A.i2);
    
                        answer.push_back(A);
                    }
 
                    int x = 3 - (i + j);
                    //wwi ke lie now nni is not present 
                    //wwi ke lie we search now nnw
                    while(!adj[i][j].empty() && !adj[j][x].empty())
                    {
                        moves A; 

                        A.i1 = *adj[i][j].begin();
                        A.i2 = *adj[j][x].begin();
                        A.c1 = s[i]; A.c2 = s[j];
 
                        adj[i][j].erase(A.i1);
                        adj[j][x].erase(A.i2);
                        adj[i][x].insert(A.i2);
 
                        answer.push_back(A);
                    }
            }
        }
 
        cout << answer.size() << "\n";
 
        for(auto &x : answer) cout << x.i1 + 1 << " " << x.c1 << " " << x.i2 + 1 << " " << x.c2 << "\n";
    }
}


Comments

Submit
0 Comments
More Questions

1189A - Keanu Reeves
678A - Johny Likes Numbers
1699C - The Third Problem
1697D - Guess The String
754B - Ilya and tic-tac-toe game
760A - Petr and a calendar
1573A - Countdown
166A - Rank List
1631B - Fun with Even Subarrays
727A - Transformation from A to B
822B - Crossword solving
1623A - Robot Cleaner
884B - Japanese Crosswords Strike Back
862B - Mahmoud and Ehab and the bipartiteness
429A - Xor-tree
1675C - Detective Task
950A - Left-handers Right-handers and Ambidexters
672B - Different is Good
1C - Ancient Berland Circus
721A - One-dimensional Japanese Crossword
1715B - Beautiful Array
60B - Serial Time
453A - Little Pony and Expected Maximum
1715A - Crossmarket
1715C - Monoblock
1512C - A-B Palindrome
1679B - Stone Age Problem
402A - Nuts
792A - New Bus Route
221A - Little Elephant and Function