_rashed

SRM522-D1-250 RowAndCoins

Jan 25th, 2023
975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. /*
  6. Ordered set usage:
  7. order_of_key (k) : Number of items strictly smaller than k.
  8. find_by_order(k) : K-th element in a set (counting from zero).
  9. */
  10.  
  11. #include <ext/pb_ds/assoc_container.hpp>
  12. #include <ext/pb_ds/tree_policy.hpp>
  13. using namespace __gnu_pbds;
  14. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  15.  
  16. const int OO = 1e9;
  17. const double EPS = 1e-9;
  18.  
  19. class RowAndCoins
  20. {
  21. public:
  22.     string s;
  23.     int mem[1000000][2];
  24.     int p[20];
  25.  
  26.     int solve(int state, int turn, int emp)
  27.     {
  28.  
  29.         if(mem[state][turn] != -1)
  30.         {
  31.             return mem[state][turn];
  32.         }
  33.  
  34.         if(emp == 1)
  35.         {
  36.             for(int i = 0; i < s.size(); i++)
  37.             {
  38.                 if(!(state&p[i]))
  39.                 {
  40.                     return s[i] == 'B';
  41.                 }
  42.             }
  43.         }
  44.         int &ret = mem[state][turn];
  45.         ret = !turn;
  46.         for(int i = 0; i < s.size(); i++) {
  47.             if(!(state&p[i])) {
  48.                 int new_state = state | p[i];
  49.                 for(int j = i; j < s.size(); j++) {
  50.                     if(!(state&p[j]) && j-i+1 < emp) {
  51.                         new_state |= p[j];
  52.                         int curr = solve(new_state,!turn,emp-(j-i+1));
  53.                         if(curr == turn) {
  54.                             ret = turn;
  55.                             return ret;
  56.                         }
  57.                     }
  58.                     else {
  59.                         break;
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         return ret;
  65.     }
  66.  
  67.     string getWinner(string cells)
  68.     {
  69.         s = cells;
  70.         int x = (1 << (cells.size()+1));
  71.         for(int i = 0; i < 20; i++) {
  72.             p[i] = (1 << i);
  73.         }
  74.         for(int i = 0; i <= x; i++)
  75.         {
  76.             mem[i][0] = mem[i][1] = -1;
  77.         }
  78.         return (solve(0,0,cells.size()) ? "Bob":"Alice");
  79.     }
  80. };
  81.  
  82. int main()
  83. {
  84.     ios_base::sync_with_stdio(NULL);
  85.     cin.tie(0);
  86.     return 0;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment