QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#245561 | #7743. Grand Finale | brothernumb2002 | WA | 35ms | 10688kb | C++17 | 1.8kb | 2023-11-10 02:01:03 | 2023-11-10 02:01:03 |
Judging History
answer
#include <bits/stdc++.h>
#define fp(i, a, b) for (int i = a, i##_ = int(b); i <= i##_; ++i)
#define fd(i, a, b) for (int i = a, i##_ = int(b); i >= i##_; --i)
using namespace std;
using ll = long long;
const int N = 5e3 + 5;
int n, m;
char s[N], t[N];
bool Chk(int k, int cntQ, int cntB) {
int i = 1, cur = n;
auto upd = [&]() {
if (t[i] == 'Q') ++cntQ;
else if (t[i] == 'B') ++cntB;
++i;
};
while (cur < k && i <= m) {
if (cntB > 0)
++cur, --cntB, upd(), upd();
else if (cntQ > 0)
++i, --cntQ, upd();
else return false;
}
if (i > m) return true;
vector<vector<int>> f(m + 1, vector<int>(k + 1, -1));
f[--i][cntB] = cntQ;
for (; i < m; ++i) fp(j, 0, k) if (~f[i][j]) {
if (j > 0) {
auto& now = f[min(i + 2, m)][j - 1 + (t[i + 1] == 'B')];
now = max(now, f[i][j] + (t[i + 1] == 'Q'));
if (i >= m - 2) return true;
}
if (f[i][j] > 0 && j + (t[i + 1] == 'B') <= k) {
auto& now = f[i + 1][j + (t[i + 1] == 'B')];
now = max(now, f[i][j] + (t[i + 1] == 'Q') - 1);
if (i == m - 1) return true;
}
}
return false;
}
void Solve() {
scanf("%d%d", &n, &m);
scanf("%s%s", s + 1, t + 1);
int cntB = 0, cntQ = 0;
fp(i, 1, n) {
if (s[i] == 'Q') ++cntQ;
if (s[i] == 'B') ++cntB;
}
int l = n, r = n + m, ans = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (Chk(mid, cntQ, cntB)) ans = mid, r = mid - 1;
else l = mid + 1;
}
if (ans == -1) puts("IMPOSSIBLE");
else printf("%d\n", ans);
}
int main() {
int t = 1;
scanf("%d", &t);
while (t--) Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4024kb
input:
2 2 6 BG BQWBWW 4 6 GQBW WWWWQB
output:
3 IMPOSSIBLE
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 4072kb
input:
2 3 8 QBG BBBWBWWW 3 8 QBG BBBWBWWW
output:
3 3
result:
ok 2 lines
Test #3:
score: -100
Wrong Answer
time: 35ms
memory: 10688kb
input:
13 184 887 WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...
output:
184 314 316 161 118 489 417 501 183 256 33 160 517
result:
wrong answer 2nd lines differ - expected: '372', found: '314'