This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma once
#include <bits/stdc++.h>
using namespace std;
#define forr(i, x , y) for(int i = x ; i<=y;i++)
#define fore(i , x) for(int i = 0 ; i<x;i++)
auto compute_dp(int n , int K , string s , vector<int> c)
{
vector<vector<int>> dp(n + 1 , vector<int>(K + 1 , 0));
vector<vector<int>> pref(2 , vector<int>(n + 1 ,0));
forr(i , 1, n)
{
if(s[i - 1] == 'X')
pref[1][i]++;
if(s[i - 1] == '_')
pref[0][i]++;
fore(j , 2)
{
pref[j][i] += pref[j][i - 1];
}
}
forr(i , 0 , n)
{
if(pref[1][i] == 0)
dp[i][0] = 1;
}
forr(i , 1 , n)
{
forr(j , 1 , K) {
int w = dp[i - 1][j];
int b = 0;
int l = i - c[j - 1] + 1;
int r = i;
if(l >= 1)
{
if(pref[0][r] - pref[0][l - 1] == 0)
{
if(i != c[j - 1])
{
if(s[i - c[j - 1]] != 'X')
{
b = dp[i - c[j - 1] - 1][j - 1];
}
}
else
b = dp[i - c[j - 1]][j - 1];
}
}
if(s[i - 1] == '_')
dp[i][j] = w;
else if(s[i - 1] == 'X')
{
dp[i][j] = b;
}
else
{
dp[i][j] = w;
dp[i][j]|=b;
}
}
}
return dp;
}
std::string solve_puzzle(std::string s, std::vector<int> c)
{
int n = (int)s.size();
int K = (int)c.size();
for(auto &C: s)
if(C == '.')C='?';
auto dpL = compute_dp(n , K, s , c);
reverse(s.begin() , s.end());
reverse(c.begin() , c.end());
auto dpR = compute_dp(n , K , s ,c);
reverse(dpR.begin() + 1 , dpR.end());
vector<int> v(K + 1 , 0);
v[0] = 1;
dpR.push_back(v);
reverse(s.begin() , s.end());
reverse(c.begin() , c.end());
vector<int> can_black(n +2 , 0);
vector<vector<int>> pref(2 ,vector<int>(n + 1 , 0));
forr(i , 1 , n)
{
pref[0][i] = pref[0][i - 1] + (s[i - 1] == '_');
pref[1][i] = pref[1][i - 1] + (s[i - 1] == 'X');
}
forr(i , 1 , n)
{
if(s[i - 1] == '?')
{
int cur = 0;
forr(k , 0 , K)
{
cur|=(dpL[i - 1][k] && dpR[i + 1][K - k]);
}
if(cur == 0)
s[i - 1] = 'X';
}
}
forr(i , 1 , n)
{
if(s[i - 1] == '?' || s[i - 1] == 'X')
{
forr(k , 1 , K)
{
int lt = i , rt = c[k - 1] + i - 1;
if(rt > n)
continue;
bool checkL = ((i == 1) || s[i - 2] != 'X');
bool checkR = ((rt == n) || s[rt] != 'X');
int dpLeft ;
if(i <= 2)
dpLeft = (i <= 2 && k == 1);
else
dpLeft = dpL[i - 2][k - 1];
int dpRight;
if(rt + 2 > n)
dpRight= (rt + 2 > n && K == k);
else
dpRight = dpR[rt + 2][K - k];
if(checkL && checkR && dpLeft && dpRight && ((pref[0][rt] - pref[0][i - 1]) == 0))
{
can_black[lt]++;
can_black[min(n + 1, rt + 1)]--;
}
}
}
}
forr(i , 1 , n)
{
can_black[i]+=can_black[i - 1];
if(can_black[i] == 0 && s[i - 1] != 'X')
s[i -1] = '_';
}
return s;
}
Compilation message (stderr)
paint.cpp:1:9: warning: #pragma once in main file
1 | #pragma once
| ^~~~
# | 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... |