# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
828415 | caganyanmaz | 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>
#include "paint.h"
#ifdef DEBUGGING
#include "../debug.h"
#else
#define debug(x...) void(42) #endif
#endif
using namespace std;
constexpr static int INF = 1e9;
constexpr static int MXN = 2e5 + 5;
constexpr static int MXK = 105;
bitset<MXK> dp[2][MXN][2]; // Valid for left/right, ending pos, whether it ends there or it ended before
int wpf[MXN], bpf[MXN];
int pf[MXN];
static inline int white(int l, int r)
{
return wpf[r] - wpf[l];
}
static inline int black(int l, int r)
{
return bpf[r] - bpf[l];
}
string solve_puzzle(string s, vector<int> c)
{
int n = s.size(), k = c.size();
debug(n, k);
for (int i = 1; i <= n; i++)
{
if (s[i-1] == '_') wpf[i]++;
else if (s[i-1] == 'X') bpf[i]++;
wpf[i] += wpf[i-1];
bpf[i] += bpf[i-1];
}
// Left iteration
dp[0][0][0][0] = 1;
for (int i = 1; i <= n; i++)
{
dp[0][i][0][0] = (dp[0][i-1][1][0] || dp[0][i-1][0][0]) && s[i-1] != 'X'; // New ended or ended before
for (int j = 1; j <= k; j++)
{
int sz = c[j-1];
if (i-sz >= 0 && dp[0][i-sz][0][j-1] && white(i-sz, i-1) == 0)
{
debug(0, i, j);
dp[0][i][1][j] = true;
}
dp[0][i][0][j] = (dp[0][i-1][1][j] || dp[0][i-1][0][j]) && s[i-1] != 'X'; // New ended or ended before
}
}
// Right iteration
dp[1][n][0][k] = 1;
for (int i = n-1; i >= 0; i--)
{
dp[1][i][0][k] = (dp[1][i+1][1][k] || dp[1][i+1][0][k]) && s[i] != 'X';
for (int j = k-1; j >= 0; j--)
{
int sz = c[j];
if (i+sz <= n && dp[1][i+sz][0][j+1] && white(i, i+sz) == 0)
{
debug(1, i, j);
dp[1][i][1][j] = true;
}
dp[1][i][0][j] = (dp[1][i+1][1][j] || dp[1][i+1][0][j]) && s[i] != 'X';
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= k-1; j++)
{
if (i+c[j] <= n && dp[0][i+c[j]][1][j+1] && dp[1][i][1][j])
{
pf[i]++;
pf[i+c[j]]--;
}
}
}
for (int i = 1; i < n; i++)
pf[i] += pf[i-1];
string res(n, '0');
for (int i = 0; i < n; i++)
{
bool white_possible = false;
for (int j = 0; j <= k; j++)
{
if ((dp[0][i+1][0][j] && dp[1][i][0][j]))
{
debug(i, j);
white_possible = true;
}
}
bool black_possible = pf[i] > 0;
debug(i, white_possible, black_possible);
if (white_possible && black_possible)
res[i] = '?';
else if (white_possible)
res[i] = '_';
else
res[i] = 'X';
assert(white_possible || black_possible);
}
return res;
}