QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#436929 | #8774. Manhattan Walk | USP_USP_USP# | WA | 28ms | 103864kb | C++23 | 5.2kb | 2024-06-09 03:51:58 | 2024-06-09 03:51:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define int ll
void dbg_out() { cerr << endl; }
template<typename H, typename... T>
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) { cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }
const int N = 21;
const int NA = 100;
const int NAC = 300;
// bitset<NAC> memo[N][N][NA];
// bool memo[N][N][NA][NAC];
// char pai[N][N][NA]
void solve() {
int n, k; cin >> n >> k;
string s; cin >> s;
if (n == 1) {
if (k != 0) {
cout << "-1";
return;
}
cout << (s == "?" ? "A" : s) << "\n";
return;
}
// no inverso vai ser CAN
// using Tbool = vector(n, vector (n, vector (n, vector<bool>())));
// using Tarray = vector(n, vector (n, vector (n, vector<array<int, 4>>())));
auto calc = [&] (string ref = "NAC", string cur) {
dbg (ref);
int n = cur.size();
vector memo (n + 1, vector(N, vector (NA, vector<bool> (NAC))));
vector pai (n + 1, vector(N, vector (NA, vector<array<int, 5>> (NAC))));
memo[0][0][0][0] = 1;
for (int i = 1; i <= n; i++) {
char u = s[i - 1];
if (u == ref[0] || u == '?') {
for (int qn = 0; qn < N; qn++)
for (int qna = 0; qna < NA; qna++)
for (int qnac = 0; qnac < NAC; qnac++) if (qn + 1 < N && memo[i - 1][qn][qna][qnac]) {
memo[i][qn + 1][qna][qnac] = 1;
pai[i][qn + 1][qna][qnac] = {i - 1, qn, qna, qnac, ref[0]};
}
}
if (u == ref[1] || u == '?') {
for (int qn = 0; qn < N; qn++)
for (int qna = 0; qna < NA; qna++)
for (int qnac = 0; qnac < NAC; qnac++) if (qna + qn < NA && memo[i - 1][qn][qna][qnac]) {
memo[i][qn][qna + qn][qnac] = 1;
pai[i][qn][qna + qn][qnac] = {i - 1, qn, qna, qnac, ref[1]};
}
}
if (u == ref[2] || u == '?') {
for (int qn = 0; qn < N; qn++)
for (int qna = 0; qna < NA; qna++)
for (int qnac = 0; qnac < NAC; qnac++) if (qnac + qna < NAC && memo[i - 1][qn][qna][qnac]) {
memo[i][qn][qna][qnac + qna] = 1;
pai[i][qn][qna][qnac + qna] = {i - 1, qn, qna, qnac, ref[2]};
}
}
if (u == '?' || (u != ref[0] && u != ref[1] && u != ref[2])) {
for (int qn = 0; qn < N; qn++)
for (int qna = 0; qna < NA; qna++)
for (int qnac = 0; qnac < NAC; qnac++) if (memo[i - 1][qn][qna][qnac]) {
memo[i][qn][qna][qnac] = 1;
pai[i][qn][qna][qnac] = {i - 1, qn, qna, qnac, 'B'};
}
}
}
return pair{memo, pai};
};
auto recover = [&] (auto &memo, auto &pai, string &r, int i, int qn, int qna, int qnac) {
string res;
while (i > 0) {
auto [ii, qqn, qqna, qqnac, c] = pai[i][qn][qna][qnac];
// dbg (i, qn, qna, qnac, c);
res += char (c);
i = ii;
qn = qqn;
qna = qqna;
qnac = qqnac;
}
reverse(all(res));
return res;
};
string s_esq = s.substr (0, n / 2);
string s_dir = s.substr (n / 2, n - n / 2);
reverse(all(s_dir));
dbg (s_esq, s_dir);
auto [memo_esq, pai_esq] = calc ("NAC", s_esq);
auto [memo_dir, pai_dir] = calc ("CAN", s_dir);
for (int qn = 0; qn < N; qn++)
for (int qna = 0; qna < NA; qna++)
for (int qnac = 0; qnac < NAC; qnac++) if (memo_esq[s_esq.size()][qn][qna][qnac]) {
for (int qqn = 0; qqn < N; qqn++)
for (int qqna = 0; qqna < NA; qqna++) {
int tot = qnac;
tot += qn * qqna;
tot += qna * qqn;
int qqnac = k - tot;
if (0 <= qqnac && qqnac < NAC && memo_dir[s_dir.size()][qqn][qqna][qqnac]) {
auto ans_esq = recover (memo_esq, pai_esq, s_esq, s_esq.size(), qn, qna, qnac);
auto ans_dir = recover (memo_dir, pai_dir, s_dir, s_dir.size(), qqn, qqna, qqnac);
reverse(all(ans_dir));
dbg ("ACHAMOS", ans_esq, ans_dir);
exit (0);
}
}
}
// auto [memo_esq, pai] = calc ("NAC", s);
// for (int qn = 0; qn < N; qn++)
// for (int qna = 0; qna < NA; qna++) if (memo[n][qn][qna][k]) {
// dbg (n, qn, qna, k);
// dbg (recover (memo, pai, s, n, qn, qna, k));
// exit (0);
// }
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 28ms
memory: 103864kb
input:
2 3 8
output:
result:
wrong output format Unexpected end of file - double expected