# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
592239 | Elias | Paint By Numbers (IOI16_paint) | C++17 | 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;
int n, k;
vector<bool> hi;
vector<int> cl;
unordered_map<int, bool> dp;
vector<int> wh, bl;
vector<int> numWhite;
bool checkWhite(int l, int r)
{
int cnt = numWhite[r - 1];
if (l)
cnt -= numWhite[l - 1];
return cnt;
}
bool DP(int i, int j)
{
if (i <= 0)
return j == 0;
if (dp.count(i + (j << 20)))
return dp[i + (j << 20)];
bool b = false;
if (j > 0)
{
int size = cl[j - 1];
if (i >= size && (i == size || !hi[i - size - 1]) && !checkWhite(i - size, i))
{
if (DP(i - size - 1, j - 1))
{
b = true;
if (i != n)
bl[i]--;
bl[i - size]++;
if (i - size > 0)
wh[i - size - 1]++;
}
}
}
if (!hi[i - 1])
{
if (DP(i - 1, j))
{
b = true;
wh[i - 1]++;
}
}
return dp[i + (j << 20)] = b;
}
string solve_puzzle(string s, vector<int> c)
{
n = s.size();
k = c.size();
cl = c;
hi = vector<bool>(n);
numWhite = vector<int>(n);
for (int i = 0; i < n; i++)
{
numWhite[i] += numWhite[max(i - 1, 0)];
if (s[i] == 'X')
hi[i] = true;
if (s[i] == '_')
numWhite[i]++;
}
wh = bl = vector<int>(n);
DP(n, k);
int black = 0;
string out;
for (int i = 0; i < n; i++)
{
black += bl[i];
if (black && wh[i])
out.push_back('?');
else if (black)
out.push_back('X');
else
out.push_back('_');
}
return out;
}
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(false);
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int> blub(k);
for (int &x : blub)
cin >> x;
cout << solve_puzzle(s, blub);
}