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 "paint.h"
#include <algorithm>
using namespace std;
const int N = 2e5 + 2;
const int K = 1e2 + 2;
int n, k, sumW[N], c[N];
bool canPref[N][K], canSuff[N][K];
string s;
bool canBlack[N], canWhite[N];
inline bool GetPref(int i, int j)
{
if (i <= 0) return j == 0;
else return s[i] != 'X' && canPref[i - 1][j];
}
inline bool GetSuff(int i, int j)
{
if (i > n) return j == k + 1;
else return s[i] != 'X' && canSuff[i + 1][j];
}
void Preprocess()
{
for (int i = 1; i <= n; ++i)
sumW[i] = sumW[i - 1] + (s[i] == '_');
canPref[0][0] = canSuff[n + 1][k + 1] = true;
for (int i = 1; i <= n; ++i)
for (int j = 0; j <= k; ++j)
{
// place white
canPref[i][j] = canPref[i - 1][j] && s[i] != 'X';
// place black
if (j > 0)
canPref[i][j] |= i >= c[j] && sumW[i] == sumW[i - c[j]] && GetPref(i - c[j], j - 1);
}
for (int i = n; i >= 1; --i)
for (int j = 1; j <= k + 1; ++j)
{
// place white
canSuff[i][j] = canSuff[i + 1][j] && s[i] != 'X';
// place black
if (j > 0)
canSuff[i][j] |= i + c[j] - 1 <= n && sumW[i - 1] == sumW[i + c[j] - 1] && GetSuff(i + c[j], j + 1);
}
}
string solve_puzzle(string _s, vector<int> _c) {
n = _s.size(), k = _c.size();
s = " " + _s, copy(_c.begin(), _c.end(), c + 1);
Preprocess();
for (int i = 1, r = 0; i <= n; ++i)
{
if (s[i] != 'X')
{
for (int j = 0; j <= k; ++j)
if (canPref[i - 1][j] && canSuff[i + 1][j + 1])
{
canWhite[i] = true;
break;
}
}
else canBlack[i] = true;
if (s[i] != '_')
{
for (int j = 1; j <= k; ++j)
if (GetPref(i - 1, j - 1) && i + c[j] - 1 <= n && sumW[i - 1] == sumW[i + c[j] - 1] && GetSuff(i + c[j], j + 1))
r = max(r, i + c[j] - 1);
if (r >= i) canBlack[i] = true;
}
else canWhite[i] = true;
}
for (int i = 1; i <= n; ++i)
if (canBlack[i] && canWhite[i]) _s[i - 1] = '?'; else
if (canBlack[i]) _s[i - 1] = 'X';
else _s[i - 1] = '_';
return _s;
}
# | 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... |