This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
std::string solve_puzzle(std::string s, std::vector <int> c) {
int n = s.size();
int k = c.size();
std::vector <std::vector <int>> dp(n, std::vector <int> (k + 1, -1));
std::vector <int> start_black(n, 0);
std::vector <int> start_white(n, 0);
std::vector <int> pre_w(n, 0);
pre_w[0] = s[0] == '_';
for (int i = 1; i < n; i++) {
pre_w[i] = pre_w[i - 1] + (s[i] == '_');
}
auto pre = [&] (const std::vector <int>& pre, int l, int r) -> int {
return pre[r] - (l ? pre[l - 1] : 0);
};
auto put = [&] (int i, int j) -> bool {
if (i + c[j] > n) {
return false;
}
if (pre(pre_w, i, i + c[j] - 1)) {
return false;
}
if (i + c[j] < n && s[i + c[j]] == 'X') {
return false;
}
return true;
};
auto f = [&] (auto&& self, int i, int j) -> bool {
if (i >= n) {
return j == k;
}
if (dp[i][j] != -1) {
return dp[i][j];
}
bool ok = false;
if (j < k && put(i, j)) {
if (self(self, i + c[j] + 1, j + 1)) {
ok = true;
start_black[i] = std::max(start_black[i], c[j]);
if (i + c[j] < n) {
start_white[i + c[j]] = std::max(start_white[i + c[j]], 1);
}
}
}
if (s[i] != 'X') {
if (self(self, i + 1, j)) {
ok = true;
start_white[i] = std::max(start_white[i], 1);
}
}
return dp[i][j] = ok;
};
f(f, 0, 0);
int cnt_b = 0, cnt_w = 0;
std::string ans(n, '$');
for (int i = 0; i < n; i++) {
cnt_b = std::max(cnt_b, start_black[i]);
cnt_w = std::max(cnt_w, start_white[i]);
if (cnt_b && cnt_w) ans[i] = '?';
else if (cnt_b) ans[i] = 'X';
else if (cnt_w) ans[i] = '_';
else assert(false);
cnt_b--;
cnt_w--;
}
return ans;
}
Compilation message (stderr)
paint.cpp: In instantiation of 'solve_puzzle(std::string, std::vector<int>)::<lambda(auto:23&&, int, int)> [with auto:23 = solve_puzzle(std::string, std::vector<int>)::<lambda(auto:23&&, int, int)>&]':
paint.cpp:54:12: required from here
paint.cpp:52:19: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
52 | return dp[i][j] = ok;
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |