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 <iostream>
#include <vector>
using namespace std;
const int MAXN=2e5+10;
const int MAXK=1e2+10;
string s;
int c[MAXK];
int lef[MAXN], ri[MAXN], pr[MAXN][MAXK];
bool ok[2][MAXN][MAXK];
int n, k;
int get_gr(int ind, int i) {
if (ind==0) return lef[i];
return n-ri[n-i+1]+1;
}
void find_dp(int ind) {
int cur;
ok[ind][0][0]=true;
for (int i=1;i<=n+1;i++) {
if (s[i]=='X') ok[ind][i][0]=false;
else ok[ind][i][0]=ok[ind][i-1][0];
for (int j=1;j<=k;j++) {
ok[ind][i][j]=false;
if (s[i]=='X') continue;
ok[ind][i][j]=ok[ind][i-1][j];
if (i-c[j]-1<0) continue;
cur=get_gr(ind,i-1);
if (cur<=i-c[j]-1) {
if (ok[ind][i-c[j]-1][j-1]) {
ok[ind][i][j]=true;
}
}
}
}
}
string solve_puzzle(string S, vector <int> C) {
n=S.size(); k=C.size();
string ans=S;
s='_'+S+'_';
for (int i=1;i<=k;i++) c[i]=C[i-1];
lef[0]=0; ri[n+1]=n+1;
for (int i=1;i<=n;i++) {
if (s[i]=='_') lef[i]=i;
else lef[i]=lef[i-1];
}
for (int i=n;i>=1;i--) {
if (s[i]=='_') ri[i]=i;
else ri[i]=ri[i+1];
}
find_dp(0);
for (int i=1;i<=n/2;i++) swap(s[i],s[n-i+1]);
for (int i=1;i<=k/2;i++) swap(c[i],c[k-i+1]);
find_dp(1);
for (int i=0;i<=n/2;i++) {
swap(s[i],s[n+1-i]);
for (int j=0;j<=k;j++) swap(ok[1][i][j],ok[1][n+1-i][j]);
}
for (int i=1;i<=k/2;i++) swap(c[i],c[k-i+1]);
for (int i=1;i<=n+1;i++) {
for (int j=0;j<=k;j++) {
bool cur;
cur=(ok[0][i][j]&ok[1][i][k-j]);
if (j>0) {
if (i-c[j]-1<0) cur=false;
else cur&=ok[0][i-c[j]-1][j-1];
}
pr[i][j]=pr[i-1][j]+cur;
}
}
for (int i=1;i<=n;i++) {
if (s[i]!='.') continue;
bool okb, okw;
okb=okw=false;
for (int j=0;j<=k;j++) {
if (ok[0][i][j] && ok[1][i][k-j]) okw=true;
if (j==0) continue;
if (ri[i]-lef[i]-1<c[j]) continue;
int l, r;
r=min(ri[i],i+c[j]);
l=max(i+1,lef[i]+c[j]+1);
if (pr[r][j]-pr[l-1][j]>0) okb=true;
}
if (okb && okw) ans[i-1]='?';
else if (okb) ans[i-1]='X';
else ans[i-1]='_';
}
return ans;
}
/*
int main () {
cout << solve_puzzle() << endl;
}*/
# | 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... |