Advertisement
K_Y_M_bl_C

Untitled

Nov 5th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. void solve (int n, int64_t k, int * answer)
  2. {
  3.     vector<vector<ll>> dp(n * 2, vector<ll>(n + 1));
  4.     dp[2 * n - 1][0] = 1;
  5.     for (int i = 2 * n - 2; i >= 0; --i)
  6.     {
  7.         for (int bal = 0; bal <= n; ++bal)
  8.         {
  9.             if (bal)
  10.                 dp[i][bal] += dp[i + 1][bal - 1];
  11.             if (bal < n)
  12.                 dp[i][bal] += dp[i + 1][bal + 1];
  13.         }
  14.     }
  15.     ll cur = k;
  16.     int cbal = 0;
  17.     string ans;
  18.     ans.reserve(2 * n);
  19.     for (int i = 0; i < 2 * n; ++i)
  20.     {
  21.         if (cbal > 0)
  22.         {
  23.             if (dp[i][cbal - 1] >= cur)
  24.             {
  25.                 --cbal;
  26.                 ans.inb(')');
  27.                 continue;
  28.             }
  29.             cur -= dp[i][cbal - 1];
  30.         }
  31.         ++cbal;
  32.         ans.inb('(');
  33.     }
  34.     vi s;
  35.     for (int i = 0; i < 2 * n; ++i)
  36.     {
  37.         if (ans[i] == '(')
  38.             s.inb(i + 1);
  39.         else
  40.             answer[s.back()] = i + 1, answer[i + 1] = s.back(), s.pop_back();
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement