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>
#define pb push_back
#define pii pair<ll, ll>
#define nyan "(=^・ω・^=)"
#define read_input freopen("in.txt","r", stdin)
#define print_output freopen("out.txt","w", stdout)
typedef long long ll;
typedef long double ld;
using namespace std;
const int maxn = 2e5+10, maxk = 103;
int n, k;
int dp1[maxn][maxk], dp2[maxn][maxk];
int dash[maxn], crs[maxn];
int lft[maxn], rgt[maxn], aux[maxn];
int p1[maxn][maxk], p2[maxn][maxk];
string str;
int x[maxn], y[maxn];
bool okx(int l, int r) {
if(l >= 2 && str[l-2] == 'X') return 0;
if(r < n && str[r] == 'X') return 0;
return (dash[r] - dash[l-1] == 0);
}
std::string solve_puzzle(std::string s, std::vector<int> c) {
n = s.length(); str = s;
k = c.size();
for(int i = 1; i <= n; i++) {
dash[i] = dash[i-1] + (str[i-1] == '_');
crs[i] = crs[i-1] + (str[i-1] == 'X');
}
dp1[0][0] = 1; p1[0][0] = 1;
for(int i = 1; i <= n && str[i-1] != 'X'; i++) p1[i][0] = 1;
for(int j = 1; j <= k; j++) {
for(int i = 1; i <= n; i++) {
int f1 = p1[i-1][j], f2;
if(i < c[j-1]) f2 = 0;
else if(j > 1) f2 = p1[i - c[j-1] - 1][j-1] & okx(i - c[j-1] + 1, i);
else f2 = p1[i - c[j-1]][j-1] & okx(i - c[j-1] + 1, i);
if(str[i-1] == '_') p1[i][j] = f1;
else if(str[i-1] == 'X') p1[i][j] = f2;
else p1[i][j] = f1 | f2;
dp1[i][j] = f2;
dp1[i][0] = p1[i][0];
}
}
reverse(str.begin(), str.end());
reverse(c.begin(), c.end());
for(int i = 1; i <= n; i++) {
dash[i] = dash[i-1] + (str[i-1] == '_');
crs[i] = crs[i-1] + (str[i-1] == 'X');
}
dp2[0][0] = 1; p2[0][0] = 1;
for(int i = 1; i <= n && str[i-1] != 'X'; i++) p2[i][0] = 1;
for(int j = 1; j <= k; j++) {
for(int i = 1; i <= n; i++) {
int f1 = p2[i-1][j], f2;
if(i < c[j-1]) f2 = 0;
else if(j > 1) f2 = p2[i - c[j-1] - 1][j-1] & okx(i - c[j-1] + 1, i);
else f2 = p2[i - c[j-1]][j-1] & okx(i - c[j-1] + 1, i);
if(str[i-1] == '_') p2[i][j] = f1;
else if(str[i-1] == 'X') p2[i][j] = f2;
else p2[i][j] = f1 | f2;
dp2[i][j] = f2;
dp2[i][0] = p2[i][0];
}
}
for(int j = 0; j <= k; j++) {
for(int i = 1; 2 * i <= n; i++) {
swap(dp2[i][j], dp2[n-i+1][j]);
swap(p2[i][j], p2[n-i+1][j]);
}
} p2[n+1][0] = p2[n+2][0] = 1;
dp2[n+1][0] = 1;
// for(int j = 0; j <= k; j++) {
// for(int i = 1; i <= n; i++)
// cout << dp1[i][j] << " ";
// cout << endl;
// } cout << endl;
// for(int j = 0; j <= k; j++) {
// for(int i = 1; i <= n; i++)
// cout << dp2[i][j] << " ";
// cout << endl;
// } cout << endl;
// for(int j = 0; j <= k; j++) {
// for(int i = 1; i <= n; i++)
// cout << p1[i][j] << " ";
// cout << endl;
// } cout << endl;
// for(int j = 0; j <= k; j++) {
// for(int i = 1; i <= n; i++)
// cout << p2[i][j] << " ";
// cout << endl;
// } cout << endl;
reverse(c.begin(), c.end());
for(int i = 1; i <= n; i++) {
if(s[i-1] == '_') continue;
for(int j = 1; j <= k; j++) {
if(dp1[i][j] & p2[i+2][k-j]) {
x[i+1]--; x[i - c[j-1] + 1]++;
}
}
}
for(int i = 1; i <= n; i++) {
if(s[i-1] == 'X') continue;
for(int j = 0; j <= k; j++) {
y[i] = max(y[i], p1[i-1][j] & p2[i+1][k-j]);
}
}
for(int i = 1; i <= n; i++) x[i] += x[i-1];
for(int i = 1; i <= n; i++) x[i] = (x[i] > 0);
// for(int i = 1; i <= n; i++) cout << x[i] << " "; cout << endl;
// for(int i = 1; i <= n; i++) cout << y[i] << " "; cout << endl;
string res = "";
for(int i = 1; i <= n; i++) {
if(s[i-1] == 'X') res += "X";
else if(s[i-1] == '_') res += "_";
else if(x[i] && y[i]) res += "?";
else if(y[i]) res += "_";
else res += "X";
}
return res;
}
# | 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... |