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>
using namespace std; using ii = pair<int,int>; using ll = long long; using vi = vector<int>;
#define rep(i,a,b) for (auto i = (a); i <= (b); ++i)
#define per(i,a,b) for (auto i = (b); i >= (a); --i)
#define all(x) begin(x), end(x)
#define siz(x) int((x).size())
#define Mup(x,y) x = max(x,y)
#define mup(x,y) x = min(x,y)
#define fi first
#define se second
#define dbg(x) cerr << #x << ": " << (x) << endl
const int N = 5005, K = 105;
int n, k;
char S[N]; int C[N];
int cX[N], c_[N];
bool pfx[N][K], sfx[N][K];
int ctr[N][K];
void calc_fx(bool fx[N][K]) {
// fx[i][j] := 1..i에서 앞선 j개의 블록 포함되게 가능 여부
fx[0][0] = true;
rep(i,1,n) {
rep(j,0,k) {
if (S[i] != 'X') fx[i][j] |= fx[i-1][j];
if (i-C[j] == 0 && j == 1) {
if (c_[C[j]] == 0) {
fx[i][j] = true;
}
}
else if (i-C[j] > 0) {
if (c_[i] - c_[i-C[j]] == 0) {
if (S[i-C[j]] != 'X') {
fx[i][j] |= fx[i-C[j]-1][j-1];
}
}
}
}
}
}
bool Pf(int i, int j) {
if (i == -1) return j == 0;
return pfx[i][j];
}
bool Sf(int i, int j) {
if (i == n+2) return j == 0;
return sfx[i][j];
}
void calc_ctr() {
rep(j,1,k) {
rep(i,0,n-C[j]) {
if (c_[i+C[j]] - c_[i] == 0) {
if (S[i] != 'X' && S[i+C[j]+1] != 'X') {
// if(i==6&&j==1)cerr<<Pf(i-1,j-1)<<' ' <<Sf(i+C[j]+2,k-j)<<endl;
ctr[i+C[j]][j] = Pf(i-1,j-1) & Sf(i+C[j]+2,k-j);
}
}
}
}
}
void precalc() {
// pfx[i][j] := 1..i에서 앞선 j개의 블록 포함되게 가능 여부
// sfx[i][j] := i..n에서 뒷쪽 j개의 블록 포함되게 가능 여부
// ctr[i][j] := k=1..i에 블록 j의 오른쪽 끝이 꽂히도록 가능한 경우의 수
calc_fx(pfx);
reverse(S+1,S+n+1);
reverse(C+1,C+k+1);
calc_fx(sfx);
reverse(S+1,S+n+1);
reverse(C+1,C+k+1);
rep(i,0,(n+1)/2) {
rep(j,0,k) swap(sfx[i][j], sfx[n-i+1][j]);
}
calc_ctr();
rep(i,1,k) rep(j,1,n) {
ctr[j][i] += ctr[j-1][i];
}
// rep(i,1,n) {
// cerr<<"sfx("<<i<<"): ";
// rep(j,0,k) {
// cerr<<sfx[i][j]<<' ';
// }
// cerr << endl;
// }
}
bool can_(int i) {
if (i == 1) return sfx[2][k];
if (i == n) return pfx[n-1][k];
bool res = false;
rep(x,0,k) res |= pfx[i-1][x] & sfx[i+1][k-x];
return res;
}
bool canX(int i) {
bool res = false;
rep(x,1,k) {
// if (i == 7)cerr<<ctr[min(n,i+C[x]-1)][x]<<' '<<ctr[i-1][x]<<endl;
res |= ctr[min(n,i+C[x]-1)][x] - ctr[i-1][x] > 0;
}
return res;
}
string solve_puzzle(string a, vi b) {
n = siz(a), k = siz(b);
string res(n,'$');
rep(i,1,n) S[i] = a[i-1];
rep(i,1,k) C[i] = b[i-1];
rep(i,1,n+2) cX[i] = cX[i-1]+(S[i]=='X');
rep(i,1,n+2) c_[i] = c_[i-1]+(S[i]=='_');
precalc();
rep(i,1,n) {
if (S[i] != '.') {
res[i-1] = S[i];
continue;
}
bool a = canX(i);
bool b = can_(i);
if (a&b) res[i-1]='?';
else if (a) res[i-1]='X';
else if (b) res[i-1]='_';
else assert(0);
}
return res;
}
#ifdef LOCAL
const int S_MAX_LEN = 200 * 1000;
char buf[S_MAX_LEN + 1];
int main() {
assert(1 == scanf("%s", buf));
std::string s = buf;
int c_len;
assert(1 == scanf("%d", &c_len));
std::vector<int> c(c_len);
for (int i = 0; i < c_len; i++) {
assert(1 == scanf("%d", &c[i]));
}
std::string ans = solve_puzzle(s, c);
printf("%s\n", ans.data());
return 0;
}
#endif
# | 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... |