QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#243823 | #7743. Grand Finale | HpSuda# | WA | 45ms | 26620kb | C++17 | 3.1kb | 2023-11-08 17:49:23 | 2023-11-08 17:49:23 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int n, m;
cin >> n >> m;
string a, b;
cin >> a >> b;
vector<vector<int>> f(m + 2, vector<int>(m + 1, n + m + 1));
for (int i = 0; i <= m; ++i) {
f[m][i] = f[m+1][i] = 0;
}
for (int i = m - 1; i >= 0; i--) {
int cur = 0;
if (b[i] == 'B') cur = 2;
else if (b[i] == 'Q') cur = 1;
for (int j = 0; j <= m; j++) {
if (!j) {
if (cur == 1) {
f[i][j] = max(1, f[i + 1][j]);
}
else if (cur == 2) {
f[i][j] = max(1, f[i + 1][j + 1] + 1);
}
else {
f[i][j] = max(1, f[i+1][j] + 1);
}
}
else {
if (j * 2 >= m - i) {
f[i][j] = 0;
}
else {
if (cur == 1) {
f[i][j] = max(0, f[i + 2][j - 1]);
f[i][j] = min(f[i][j], max(1, f[i + 1][j]));
}
else if (cur == 2) {
f[i][j] = f[i + 2][j];
f[i][j] = min(f[i][j], f[i + 1][j + 1] + 1);
}
else {
f[i][j] = f[i + 2][j - 1];
f[i][j] = min(f[i][j], f[i + 1][j] + 1);
}
}
}
}
}
int c1 = 0, c2 = 0;
int sum = n;
for (int i = 0; i < n; i++) {
if (a[i] == 'Q') ++c1;
else if (a[i] == 'B') ++c2;
}
auto check = [&](int x) -> bool {
int cur = 0;
int s1 = c1, s2 = c2;
while (cur < m && sum < x) {
if (s2) {
s2--;
if (b[cur] == 'B') {
s2++;
}
else if (b[cur] == 'Q') {
s1++;
}
sum++;
cur++;
if (cur == m) break;
if (b[cur] == 'B') {
s2++;
}
else if (b[cur] == 'Q') {
s1++;
}
}
else if (s1) {
s1--;
if (b[cur] == 'B') {
s2++;
}
else if (b[cur] == 'Q') {
s1++;
}
cur++;
}
else {
break;
}
}
if (cur == m) return true;
if (!s1 && !s2) return false;
if (s2 * 2 + s1 >= m - cur) return true;
return f[cur][s2] <= s1;
};
for (int i = n; i <= n + m; i++) {
if (check(i)) {
cout << i << "\n";
return;
}
}
cout << "IMPOSSIBLE\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3464kb
input:
2 2 6 BG BQWBWW 4 6 GQBW WWWWQB
output:
3 IMPOSSIBLE
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 3412kb
input:
2 3 8 QBG BBBWBWWW 3 8 QBG BBBWBWWW
output:
3 3
result:
ok 2 lines
Test #3:
score: -100
Wrong Answer
time: 45ms
memory: 26620kb
input:
13 184 887 WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...
output:
IMPOSSIBLE IMPOSSIBLE IMPOSSIBLE 161 IMPOSSIBLE IMPOSSIBLE IMPOSSIBLE IMPOSSIBLE IMPOSSIBLE IMPOSSIBLE IMPOSSIBLE 160 IMPOSSIBLE
result:
wrong answer 1st lines differ - expected: '184', found: 'IMPOSSIBLE'