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>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
string solve_puzzle(string s, vector<int> c) {
int n = (int) s.size();
int m = (int) c.size();
string res = s;
vector dp(2, vector(m + 1, vector<int>(n)));
for (int z = 0; z < 2; z++) {
vector<int> pref(n + 1);
for (int i = 0; i < n; i++) {
pref[i + 1] = pref[i] + (s[i] == '_');
}
vector<vector<int>> sum(m + 1, vector<int>(n + 1));
for (int i = 0; i < m; i++) {
int lst = -1;
for (int j = 0; j + c[i] <= n; j++) {
sum[i + 1][j + c[i]] = sum[i + 1][j + c[i] - 1];
if (j > 0 && s[j - 1] == 'X') {
if (i == 0) {
for (int k = j + c[i] + 1; k <= n; k++) {
sum[1][k] = sum[1][k - 1];
}
break;
}
lst = j - 1;
continue;
}
if (j + c[i] < n && s[j + c[i]] == 'X') {
continue;
}
if (pref[j] != pref[j + c[i]]) {
continue;
}
if (i == 0 || sum[i][max(0, lst)] != sum[i][max(0, j - 1)]) {
dp[z][i + 1][j + c[i] - 1] = 1;
sum[i + 1][j + c[i]]++;
}
}
}
if (s[0] != 'X') {
dp[z][0][0] = 1;
}
for (int i = 0; i <= m; i++) {
for (int j = 1; j < n; j++) {
if (dp[z][i][j - 1] && s[j] != 'X') {
dp[z][i][j] = 1;
}
}
}
reverse(s.begin(), s.end());
reverse(c.begin(), c.end());
}
reverse(dp[1].begin(), dp[1].end());
for (int i = 0; i <= m; i++) {
reverse(dp[1][i].begin(), dp[1][i].end());
}
vector<int> pref(n + 1);
for (int i = 0; i < n; i++) {
pref[i + 1] = pref[i] + (s[i] == '_');
}
vector<int> sum(n + 1);
for (int i = 0; i < m; i++) {
for (int j = 0; j + c[i] <= n; j++) {
if (j > 0 && s[j - 1] == 'X') {
continue;
}
if (j + c[i] < n && s[j + c[i]] == 'X') {
continue;
}
if (pref[j] != pref[j + c[i]]) {
continue;
}
if (j >= 2 && !dp[0][i][j - 2]) {
continue;
}
if (j < 2 && i != 0) {
continue;
}
if (j + c[i] + 1 < n && !dp[1][i + 1][j + c[i] + 1]) {
continue;
}
if (j + c[i] + 1 >= n && i != m - 1) {
continue;
}
sum[j]++;
sum[j + c[i]]--;
}
}
for (int i = 0; i < n; i++) {
sum[i + 1] += sum[i];
if (res[i] != '.') {
continue;
}
if (sum[i] == 0) {
res[i] = '_';
continue;
}
res[i] = 'X';
if (i == 0) {
if (n > 1 && dp[1][0][1]) {
res[i] = '?';
}
continue;
} else if (i == n - 1) {
if (n > 1 && dp[0][m][n - 2]) {
res[i] = '?';
}
break;
}
for (int j = 0; j <= m; j++) {
if (dp[0][j][i - 1] && dp[1][j][i + 1]) {
res[i] = '?';
}
}
}
return res;
}
#ifdef tabr
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
debug(solve_puzzle("..........", {3, 4}));
debug(solve_puzzle("........", {3, 4}));
debug(solve_puzzle("..._._....", {3}));
debug(solve_puzzle(".X........", {3}));
debug(solve_puzzle("....X..X...", {3, 3}));
debug(solve_puzzle("..X_.._X...", {1, 2, 1}));
debug(solve_puzzle(".._.X_.._.._X.", {2, 2, 2}));
debug(solve_puzzle("..X_.._X....", {1, 2, 2}));
debug(solve_puzzle(".X.._.......", {1, 2, 3}));
return 0;
}
#endif
# | 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... |