QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#345523 | #7743. Grand Finale | Sorting# | TL | 1ms | 3692kb | C++20 | 4.1kb | 2024-03-07 04:57:12 | 2024-03-07 04:57:14 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vpii = vector<pii>;
using vvi = vector<vi>;
const int N = 5005;
int dp1[N][N];
int dp2[N][N];
const int INF = 1e9 + 7;
void solve() {
int n, m;
cin >> n >> m;
string hand, draw;
cin >> hand >> draw;
for(int i = 0; i <= m + 5; i++) {
for(int j = 0; j <= n+m + 5; j++) {
dp1[i][j] = -INF;
dp2[i][j] = INF;
}
}
int hand_q = 0, hand_b = 0, hand_w = 0;
for(char c : hand) {
if(c == 'Q') {
hand_q++;
} else if(c == 'B') {
hand_b++;
} else {
hand_w++;
}
}
vi draw_q(m+1), draw_b(m+1), draw_w(m+1);
for(int i = 0; i < m; i++) {
draw_q[i+1] = draw_q[i];
draw_b[i+1] = draw_b[i];
draw_w[i+1] = draw_w[i];
if(draw[i] == 'Q') {
draw_q[i+1]++;
} else if(draw[i] == 'B') {
draw_b[i+1]++;
} else {
draw_w[i+1]++;
}
}
dp1[0][hand_q] = hand_b;
for(int i = 0; i < m; i++) {
for(int q = 0; q <= (n+m); q++) {
if(dp1[i][q] == -INF) continue;
int dq = draw[i] == 'Q';
int db = draw[i] == 'B';
// play a Q
if(q > 0) {
dp1[i+1][q-1+dq] = max(dp1[i+1][q-1+dq], dp1[i][q] + db);
}
// play a B
if(dp1[i][q] > 0) {
if(i + 1 < m) {
dq += draw[i+1] == 'Q';
db += draw[i+1] == 'B';
}
int j = min(i+2, m);
dp1[j][q+dq] = max(dp1[j][q+dq], dp1[i][q] + db - 1);
}
}
}
for(int q = 0; q <= n+m; q++) {
dp2[m][q] = 0;
}
for(int i = m; i > 0; i--) {
for(int q = 0; q <= (n+m); q++) {
if(dp2[i][q] == INF) continue;
int b = dp2[i][q];
// played a Q
{
int q0 = q + 1 - (draw[i-1] == 'Q');
int b0 = b - (draw[i-1] == 'B');
if(q0 > 0 && b0 >= 0) {
dp2[i-1][q0] = min(dp2[i-1][q0], b0);
}
}
// played a B into empty deck
if(i == m) {
int q0 = q - (draw[i-1] == 'Q');
int b0 = b + 1 - (draw[i-1] == 'B');
if(b0 > 0 && q0 >= 0) {
dp2[i-1][q0] = min(dp2[i-1][q0], b0);
}
}
// played a B
if(i >= 2) {
int q0 = q - (draw[i-2] == 'Q');
int b0 = b + 1 - (draw[i-2] == 'B');
if(b0 > 0 && q0 >= 0) {
dp2[i-2][q0] = min(dp2[i-2][q0], b0);
}
}
}
}
auto ok = [&](int k) {
for(int s = 0; s <= m; s++) {
int b0 = k - n;
int q0 = s - 2 * b0;
if(b0 < 0 || q0 < 0) continue;
int B = hand_b + draw_b[s] - b0;
int Q = hand_q + draw_q[s] - q0;
if(B < 0 || Q < 0) continue;
cerr << k << ' ' << s << ' ' << b0 << ' ' << q0 << ' ' << B << ' ' << Q << ' ' << dp1[s][Q] << ' ' << dp2[s][Q] << endl;
if(dp1[s][Q] >= B && dp2[s][Q] <= B) {
return true;
}
}
return false;
};
for(int k = 0; k <= (n+m); k++) {
if(ok(k)) {
cout << k << endl;
return;
}
}
cout << "IMPOSSIBLE" << endl;
// int lo = 0, hi = (n+m);
// while(lo + 1 < hi) {
// int k = (lo + hi) / 2;
// if(ok(k+1)) {
// hi = k;
// } else {
// lo = k;
// }
// }
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tc;
cin >> tc;
while(tc--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3692kb
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: 3648kb
input:
2 3 8 QBG BBBWBWWW 3 8 QBG BBBWBWWW
output:
3 3
result:
ok 2 lines
Test #3:
score: -100
Time Limit Exceeded
input:
13 184 887 WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...
output:
184 372 316 161 118 536