Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void solve (int n, int64_t k, int * answer)
- {
- vector<vector<ll>> dp(n * 2, vector<ll>(n + 1));
- dp[2 * n - 1][0] = 1;
- for (int i = 2 * n - 2; i >= 0; --i)
- {
- for (int bal = 0; bal <= n; ++bal)
- {
- if (bal)
- dp[i][bal] += dp[i + 1][bal - 1];
- if (bal < n)
- dp[i][bal] += dp[i + 1][bal + 1];
- }
- }
- ll cur = k;
- int cbal = 0;
- string ans;
- ans.reserve(2 * n);
- for (int i = 0; i < 2 * n; ++i)
- {
- if (cbal > 0)
- {
- if (dp[i][cbal - 1] >= cur)
- {
- --cbal;
- ans.inb(')');
- continue;
- }
- cur -= dp[i][cbal - 1];
- }
- ++cbal;
- ans.inb('(');
- }
- vi s;
- for (int i = 0; i < 2 * n; ++i)
- {
- if (ans[i] == '(')
- s.inb(i + 1);
- else
- answer[s.back()] = i + 1, answer[i + 1] = s.back(), s.pop_back();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement