# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
507641 | sliviu | Paint By Numbers (IOI16_paint) | C++14 | 0 ms | 0 KiB |
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;
string solve_puzzle(string s, const vector<int>& c) {
string ans = "";
int n = s.length(), k = c.size();
s = '$' + s;
vector<int> canw(n + 2), canb(n + 2), cnt(n + 1);
vector<vector<int>> dpb(n + 2, vector<int>(k + 2)), dpa(n + 2, vector<int>(k + 2));
for (int i = 1; i <= n + 1; ++i)
cnt[i] = cnt[i - 1] + (s[i] == '_');
dpb[0][0] = dpa[n + 1][k] = 1;
for (int i = 0; i < n; ++i)
for (int j = 0; j <= k; ++j) {
if (s[i + 1] != 'X')
dpb[i + 1][j] |= dpb[i][j];
if (j < k&& i + c[j] + 1 <= n && s[i + c[j] + 1] != 'X' && cnt[i] == cnt[i + c[j]])
dpb[i + c[j] + 1][j + 1] |= dpb[i][j];
}
for (int i = n + 1; i > 1; --i)
for (int j = 0; j <= k; ++j) {
if (s[i - 1] != 'X')
dpa[i - 1][j] |= dpa[i][j];
if (j && i - c[j - 1] - 1 >= 1 && s[i - c[j - 1] - 1] != 'X' && cnt[i] == cnt[i - c[j - 1]])
dpa[i - c[j - 1] - 1][j - 1] |= dpa[i][j];
}
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= k; ++j) {
if (s[i] != 'X' && dpb[i][j] && dpa[i][j])
canw[i] |= 1;
if (j < k && i + c[j] <= n && dpb[i][j] && dpa[i + c[j] + 1][j + 1] && cnt[i] == cnt[i + c[j]])
++canb[i + 1], --canb[i + c[j] + 1];
}
for (int i = 1; i <= n; ++i) {
canb[i] += canb[i - 1];
if (canw[i] && canb[i])
ans += '?';
else
ans += (canw[i] ? '_' : 'X');
}
cout << ans << " | ";
return ans;
}