# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
146846 | abacaba | 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.
const int N = 2e5 + 15;
const int K = 1e2 + 15;
int n, k, p[2][N], a[K], pref[2][N], dp[N][K];
int rec(int pos, int block) {
if(block == k)
return dp[pos][block] = 1;
if(dp[pos][block])
return dp[pos][block];
dp[pos][block] = 2;
for(int i = pos + a[block] + 1; i + a[block + 1] - 1 <= n; ++i)
if(p[0][i + a[block+1] - 1] - p[0][i-1] == 0){
if(p[1][i-1] - p[1][pos + a[block] - 1] == 0) {
if(rec(i, block + 1) == 1) {
return dp[pos][block] = 1;
}
}
}
return dp[pos][block];
}
string solve_puzzle(string s, vector<int> c) {
string ans = "";
n = s.size(); k = c.size();
s = '#' + s;
for(int i = 1; i <= n; ++i)
p[0][i] = p[0][i-1] + (s[i] == '_'),
p[1][i] = p[1][i-1] + (s[i] == 'X');
for(int i = 0; i < k; ++i)
a[i+1] = c[i];
for(int i = 1; i <= n; ++i)
if(!p[1][i-1] && i + a[1] - 1 <= n && p[0][i + a[1] - 1] - p[0][i - 1] == 0)
rec(i, 1);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= k; ++j) {
if(dp[i][j] == 1) {
if(j == 1)
++pref[0][1],
--pref[0][i];
if(j == k)
++pref[0][a[j] + i];
++pref[1][i];
--pref[1][a[j] + i];
++pref[0][i - 1];
--pref[0][i];
++pref[0][a[j] + i];
--pref[0][a[j] + i + 1];
}
}
}
for(int i = 1; i <= n; ++i)
for(int j = 0; j < 2; ++j)
pref[j][i] += pref[j][i-1];
for(int i = 1; i <= n; ++i) {
if(pref[1][i] && pref[0][i])
ans += '?';
else if(pref[1][i])
ans += 'X';
else
ans += '_';
}
return ans;
}