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 GCC diagnostic warning "-std=c++11"
#include <bits/stdc++.h>
#include "paint.h"
#define pb push_back
#define MOD 1000000007
#define flush fflush(stdout)
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define pii pair<int,int>
using namespace std;
const int N=200005,K=105;
bool dpPref[N][K],dpSuf[N][K],dp2[N][K];
int lastBlackPref[N],lastBlackSuf[N],lastDpPref[N][K],lastDpSuf[N][K],lastDp2[N][K],L,R;
string solve_puzzle(string s, vector<int> c) {
int n=s.size(); int k=c.size();
// build lastBlackPref;
if (s[0]=='X') lastBlackPref[0]=0;
else lastBlackPref[0]=-1e9;
for (int i=1; i<n; i++) {
lastBlackPref[i]=lastBlackPref[i-1];
if (s[i]=='X') lastBlackPref[i]=i;
}
// build lastBlackSuf;
if (s[n-1]=='X') lastBlackSuf[n-1]=n-1;
else lastBlackSuf[n-1]=1e9;
for (int i=n-2; i>=0; i--) {
lastBlackSuf[i]=lastBlackSuf[i+1];
if (s[i]=='X') lastBlackSuf[i]=i;
}
// build dpPref
int firstBlack=lastBlackSuf[0];
int lastWhite=-1;
for (int i=0; i<=min(n-1,firstBlack+c[0]-1); i++) {
if (s[i]=='_') lastWhite=i;
if (i-c[0]+1>lastWhite) dpPref[i][0]=1;
}
int lastt=-1;
for (int i=0; i<n; i++) {
if (dpPref[i][0]==1) lastt=i;
lastDpPref[i][0]=lastt;
}
for (int ind=1; ind<k; ind++) {
lastWhite=-1e9;
int last=-1;
for (int i=0; i<n; i++) {
lastDpPref[i][ind]=last;
if (s[i]=='_') lastWhite=i;
if (i-c[ind]+1<=lastWhite) continue;
if ((i-c[ind])-1<0) continue;
if (s[i-c[ind]]=='X') continue;
L=max(0,lastBlackPref[(i-c[ind])-1]); R=(i-c[ind])-1;
if (L<=R && lastDpPref[R][ind-1]>=L) dpPref[i][ind]=1;
if (dpPref[i][ind]==1) last=i;
lastDpPref[i][ind]=last;
}
}
// build dpSuf
int lastBlack=lastBlackPref[n-1];
lastWhite=n;
for (int i=n-1; i>=max(0,lastBlack-c[k-1]+1); i--) {
if (s[i]=='_') lastWhite=i;
if (i+c[k-1]-1<lastWhite) dpSuf[i][k-1]=1;
}
int lasttt=n;
for (int i=n-1; i>=0; i--) {
if (dpSuf[i][k-1]==1) lasttt=i;
lastDpSuf[i][k-1]=lasttt;
}
for (int ind=k-2; ind>=0; ind--) {
lastWhite=1e9;
int last=n;
for (int i=n-1; i>=0; i--) {
lastDpSuf[i][ind]=last;
if (s[i]=='_') lastWhite=i;
if (i+c[ind]-1>=lastWhite) continue;
if ((i+c[ind])+1>n-1) continue;
if (s[(i+c[ind])]=='X') continue;
L=(i+c[ind])+1; R=min(n-1,lastBlackSuf[(i+c[ind])+1]);
if (L<=R && lastDpSuf[L][ind+1]<=R) dpSuf[i][ind]=1;
if (dpSuf[i][ind]) last=i;
lastDpSuf[i][ind]=last;
}
}
//build dp2 and lastDp2
for (int ind=0; ind<k; ind++) {
int last=-1e9;
for (int i=0; i<n; i++) {
int j=i+c[ind]-1;
if (j<=n-1 && dpSuf[i][ind]==1 && dpPref[j][ind]==1) {
dp2[i][ind]=1;
last=i;
}
lastDp2[i][ind]=last;
}
}
// build answer
string ans=s;
for (int i=0; i<n; i++) {
if (s[i]!='.') continue;
bool canBeWhite=0,canBeBlack=0;
// can be '_'
if (lastBlackPref[i]<0) {
L=i+1; R=min(n-1,lastBlackSuf[i]);
if (L<=R && lastDpSuf[L][0]<=R) canBeWhite=1;
}
for (int ind=0; ind<k-1; ind++) {
bool B1=0,B2=0;
L=max(0,lastBlackPref[i]); R=i-1;
if (L<=R && lastDpPref[R][ind]>=L) B1=1;
L=i+1; R=min(n-1,lastBlackSuf[i]);
if (L<=R && lastDpSuf[L][ind+1]<=R) B2=1;
if (B1 && B2) {
canBeWhite=1; break;
}
}
if (lastBlackSuf[i]>n-1) {
L=max(0,lastBlackPref[i]); R=i-1;
if (L<=R && lastDpPref[R][k-1]>=L) canBeWhite=1;
}
// can be 'X'
for (int ind=0; ind<k; ind++) {
L=i-c[ind]+1; R=i;
if (lastDp2[R][ind]>=L) {
canBeBlack=1; break;
}
}
if (canBeBlack==1 && canBeWhite==1) ans[i]='?';
else if (canBeBlack==1) ans[i]='X';
else ans[i]='_';
}
return ans;
}
Compilation message (stderr)
paint.cpp:1:32: warning: '-std=c++11' is not an option that controls warnings [-Wpragmas]
1 | #pragma GCC diagnostic warning "-std=c++11"
| ^~~~~~~~~~~~
# | 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... |