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>
using namespace std;
void make_dp_front(string& s, vector<int>& c, vector<vector<array<bool, 2> > >& dp){
int n=s.size();
int k=c.size();
vector<int> lastwhite(n);
int last=-10;
for(int i=0; i<n; i++){
if(s[i]=='_'){
last=i;
}
lastwhite[i]=last;
}
dp[0][0][0]=true;
for(int i=1; i<=n; i++){
for(int j=0; j<=k; j++){
if(s[i-1]=='_'){//white
dp[i][j][0]=dp[i-1][j][0] || dp[i-1][j][1];
dp[i][j][1]=false;
}
else if(s[i-1]=='X'){ //black
dp[i][j][0]=false;
if(0<j && 0<=i-c[j-1] && lastwhite[i-1]+1<=i-c[j-1]){
dp[i][j][1]=dp[i-c[j-1]][j-1][0];
}
}
else{
dp[i][j][0]=dp[i-1][j][0] || dp[i-1][j][1];
if(0<j && 0<=i-c[j-1] && lastwhite[i-1]+1<=i-c[j-1]){
dp[i][j][1]=dp[i-c[j-1]][j-1][0];
}
}
}
}
}
void make_dp_back(string& s, vector<int>& c, vector<vector<array<bool, 2> > >& dp){
int n=s.size();
int k=c.size();
vector<int> lastwhite(n);
int last=n+10;
for(int i=n-1; i>=0; i--){
if(s[i]=='_'){
last=i;
}
lastwhite[i]=last;
}
dp[n][k][0]=true;
for(int i=n-1; i>=0; i--){
for(int j=k; j>=0; j--){
if(s[i]=='_'){//white
dp[i][j][0]=dp[i+1][j][0] || dp[i+1][j][1];
dp[i][j][1]=false;
}
else if(s[i]=='X'){ //black
dp[i][j][0]=false;
if(j<k && i+c[j]<=n && i+c[j]<=lastwhite[i]){
dp[i][j][1]=dp[i+c[j]][j+1][0];
}
}
else{
dp[i][j][0]=(dp[i+1][j][0] || dp[i+1][j][1]);
if(j<k && i+c[j]<=n && i+c[j]<=lastwhite[i]){
dp[i][j][1]=dp[i+c[j]][j+1][0];
}
}
}
}
}
string solve_puzzle(string s, vector<int> c){
int n=s.size();
int k=c.size();
vector<vector<array<bool, 2> > > dp_front(n+1, vector<array<bool, 2> > (k+1));
make_dp_front(s, c, dp_front);
vector<vector<array<bool, 2> > > dp_back(n+1, vector<array<bool, 2> > (k+1));
make_dp_back(s, c, dp_back);
vector<int> lastwhite(n+1);
int last=-10;
for(int i=0; i<=n; i++){
lastwhite[i]=last;
if(i<n && s[i]=='_'){
last=i;
}
}
vector<bool> black(n);
vector<bool> white(n);
for(int j=0; j<k; j++){
int start=0;
for(int l=0, r=c[j]; r<=n; l++, r++){
bool ok=(lastwhite[r]<l);
if(ok && dp_front[l][j][0] && dp_back[r][j+1][0]){
start=max(start, l);
for(int i=start; i<r; i++){
black[i]=true;
}
start=r;
}
}
}
lastwhite.clear();
for(int j=0; j<=k; j++){
for(int i=0; i<n; i++){
if((dp_front[i][j][0] || dp_front[i][j][1])
&& (dp_back[i+1][j][0] || dp_back[i+1][j][1])){
white[i]=true;
}
}
}
string ans=s;
for(int i=0; i<n; i++){
assert(black[i] || white[i]);
if(s[i]=='_' || !black[i]){
ans[i]='_';
}
else if(s[i]=='X' || !white[i]){
ans[i]='X';
}
else{
ans[i]='?';
}
}
return ans;
}
# | 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... |