Advertisement
Ankit_132

D

Feb 20th, 2024
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. typedef long long ll;
  3. #define pb push_back
  4. #define ff first
  5. #define ss second
  6. const int N=2e3+7;
  7. const int mod=1e9+7;
  8.  
  9. int32_t main()
  10. {
  11.    ios_base::sync_with_stdio(false);
  12.    cin.tie(0);
  13.  
  14.    int t;
  15.    cin >> t;
  16.    while(t--)
  17.    {
  18.         int n;
  19.         cin >> n;
  20.  
  21.         char trump;
  22.         cin >> trump;
  23.  
  24.         vector<string> cards,trumpCards;
  25.         for(int i=0;i<2*n;i++)
  26.         {
  27.             string card;
  28.             cin >> card;
  29.  
  30.             if(card[1]==trump)
  31.                 trumpCards.pb(card);
  32.             else
  33.                 cards.pb(card);
  34.         }
  35.  
  36.         vector< pair<string,string> > moves;
  37.         for(int i=0;i<cards.size();i++)
  38.         {
  39.             for(int j=0;j<cards.size();j++)
  40.             {
  41.                 if(i==j)
  42.                     continue;
  43.                 if(cards[i]=="")
  44.                     continue;
  45.                 if(cards[j]=="")
  46.                     continue;
  47.  
  48.                 if(cards[i][1]==cards[j][1])
  49.                 {
  50.                     moves.pb({min(cards[i],cards[j]),max(cards[i],cards[j])});
  51.                     cards[i]="";
  52.                     cards[j]="";
  53.                 }
  54.             }
  55.         }
  56.  
  57.         bool ok=true;
  58.         for(int i=0;i<cards.size();i++)
  59.         {
  60.             if(cards[i]!="")
  61.             {
  62.                 if(trumpCards.empty())
  63.                     ok=false;
  64.                 else
  65.                 {
  66.                     moves.pb({cards[i],trumpCards.back()});
  67.                     trumpCards.pop_back();
  68.                 }
  69.             }
  70.         }
  71.  
  72.         if(trumpCards.size()%2==1)
  73.             ok=false;
  74.  
  75.         if(!ok)
  76.         {
  77.             cout << "IMPOSSIBLE\n\n";
  78.             continue;
  79.         }
  80.  
  81.         sort(trumpCards.begin(),trumpCards.end());
  82.         for(int i=0;i<trumpCards.size();i++)
  83.         {
  84.             moves.pb({trumpCards[i],trumpCards.back()});
  85.             trumpCards.pop_back();
  86.         }
  87.        
  88.         for(auto ele : moves)
  89.             cout << ele.first << " " << ele.second << "\n";
  90.         cout << "\n";
  91.  
  92.    }  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement