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];
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] + (s[i-1] == '_');
for(int i = 1; i <= n; i++) crs[i] = crs[i-1] + (s[i-1] == 'X');
aux[0] = 1; dp1[0][0] = 1;
for(int i = 1; i <= n && str[i-1] != 'X'; i++) aux[i] = 1, dp1[i][0] = 1;
for(int j = 1, pos = 0; j <= k; j++) {
for(int i = 1; i <= n; i++) {
if(i < c[j-1]) continue;
if(j > 1) dp1[i][j] = aux[i - c[j-1] - 1] & okx(i - c[j-1] + 1, i);
else dp1[i][j] = aux[i - c[j-1]] & okx(i - c[j-1] + 1, i);
dp1[i][j] = dp1[i][j] & (crs[max(i - c[j-1] - 1, 0)] == crs[pos]);
}
aux[0] = 0;
for(int i = 1; i <= n; i++) {
aux[i] = max(aux[i-1], dp1[i][j]);
if(dp1[i][j]) pos = i;
}
}
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] == '_');
for(int i = 1; i <= n; i++) crs[i] = crs[i-1] + (str[i-1] == 'X');
aux[0] = 1; dp2[0][0] = 1;
for(int i = 1; i <= n && str[i-1] != 'X'; i++) aux[i] = 1, dp2[i][0] = 1;
for(int j = 1, pos = 0; j <= k; j++) {
for(int i = 1; i <= n; i++) {
if(i < c[j-1]) continue;
if(j > 1) dp2[i][j] = aux[i - c[j-1] - 1] & okx(i - c[j-1] + 1, i);
else dp2[i][j] = aux[i - c[j-1]] & okx(i - c[j-1] + 1, i);
dp2[i][j] = dp2[i][j] & (crs[max(i - c[j-1] - 1, 0)] == crs[pos]);
}
aux[0] = 0;
for(int i = 1; i <= n; i++) {
aux[i] = max(aux[i-1], dp2[i][j]);
if(dp2[i][j]) pos = i;
}
}
for(int j = 1; j <= k; j++) {
for(int i = 1; i <= n; i++)
dp2[i][j] = max(dp2[i-1][j], dp2[i][j]);
}
for(int j = 0; j <= k; j++) {
for(int i = 1; 2*i <= n; i++) swap(dp2[i][j], dp2[n-i+1][j]);
dp2[n+1][0] = 1;
}
reverse(c.begin(), c.end());
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= k; j++) {
if(i == n) {
if(dp1[i][j] & dp2[i+1][k-j]) x[i+1]--, x[i - c[j-1] + 1]++;
}
else {
if(dp1[i][j] && dp2[i+2][k-j]) x[i+1]--, x[i - c[j-1] + 1]++;
}
}
}
for(int j = 1; j <= k; j++) {
for(int i = 1; i <= n; i++)
dp1[i][j] = max(dp1[i-1][j], dp1[i][j]);
}
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= k; j++) {
y[i] = max(y[i], dp1[i-1][j] & dp2[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);
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... |