QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#343918 | #7743. Grand Finale | ucup-team992# | TL | 4ms | 52896kb | C++20 | 3.2kb | 2024-03-03 09:11:58 | 2024-03-03 09:12:02 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll int
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
int main() {
vector<vector<bool>> DP0(2501, vector<bool>(2501, false));
vector<vector<ll>> DP1(2501, vector<ll>(5001, -1));
vector<ll> BPS(2501,0);
vector<ll> QPS(2501,0);
ll T; cin >> T;
for(int t = 0; t < T; t++) {
ll N, M; cin >> N >> M;
string H,P;
cin >> H;
cin >> P;
ll B0=0, Q0=0;
for(int i = 0; i < N; i++) {
if(H[i] == 'B') B0++;
else if(H[i] == 'Q') Q0++;
}
BPS.assign(M,0);
QPS.assign(M,0);
for(int j = 0; j < M; j++) {
if(P[j] == 'B') BPS[j+1]++;
else if(P[j] == 'Q') QPS[j+1]++;
BPS[j+1] += BPS[j]; QPS[j+1] += QPS[j];
}
auto query = [&](ll K) {
//cout << "QUERYING " << K << "\n\n";
for(int i = 0; i <= M; i++) DP0[i].assign(K+1, false);
for(int i = 0; i <= M; i++) DP1[i].assign(M+N+1,-1);
DP0[0][0] = true;
for(int i = 1; i <= M; i++) {
for(int b = 0; (b <= K) && (2*b <= i); b++) {
// (i-1,b) -> (i, b)
{
ll qthen = QPS[i-1] + Q0;
ll qspent = i-1 - 2*b;
if((qthen - qspent > 0) && DP0[i-1][b]) DP0[i][b] = true;
}
// (i-2, b-1) -> (i,b)
if(i-2>=0 && b-1>=0) {
ll bthen = BPS[i-2] + B0;
ll bspent = b-1;
if((bthen - bspent)>0 && DP0[i-2][b-1]) DP0[i][b] = true;
}
//cout << i << " " << b << " " << DP0[i][b] << endl;
if(DP0[i][b] && i == M) return true;
ll bnow = BPS[i]+B0-b;
if(bnow > 0 && i+2 >= M && DP0[i][b]) return true;
if(DP0[i][b] && b == K) {
// handsize is full, transition to second-state.
ll qnow = QPS[i] + Q0;
ll qspent = i - 2*b;
ll bnow = BPS[i]+B0-b;
DP1[i][bnow] = max(DP1[i][bnow], qnow-qspent);
}
}
}
//cout << "GOTO DP2\n";
if(K == 0) DP1[0][B0] = Q0;
for(int i = 1; i <= M; i++) {
for(int b = 0; b <= N+M; b++) {
// we are at full hand-size.
//cout << i << " " << b << " " << DP1[i][b] << endl;
// played a Q to get here.
{
ll bthen = b;
if(P[i-1] == 'B') bthen--;
ll cost = -1;
if(P[i-1] == 'Q') cost = 0;
if(bthen >= 0 && DP1[i-1][bthen]>0) {
DP1[i][b] = max(DP1[i][b], DP1[i-1][bthen]+cost);
}
}
//cout << i << " " << b << " " << DP1[i][b] << endl;
// played a B to get here.
if(i-2 >= 0) {
//cout << P[i-2] << " ";
ll bthen = b+1;
if(P[i-2] == 'B') bthen--;
ll cost = 0;
if(P[i-2] == 'Q') cost++;
//cout << bthen << " ";
if(bthen > 0 && bthen <= N+M && DP1[i-2][bthen]>=0) {
DP1[i][b] = max(DP1[i][b], DP1[i-2][bthen]+cost);
}
}
//cout << i << " " << b << " " << DP1[i][b] << endl;
if(i+2 >= M && b > 0 && DP1[i][b] >= 0) return true;
if(DP1[i][b]>=0 && i==M) return true;
}
}
//cout << "Failed.\n";
return false;
};
if(!query(M)) {
cout << "IMPOSSIBLE\n"; continue;
}
ll kl = 0;
ll kh = M;
while(kl < kh) {
ll km = (kl+kh)/2;
if(query(km)) kh = km;
else kl = km+1;
}
cout << kh+N << endl;
}
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 52772kb
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: 52896kb
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 534 IMPOSSIBLE 631 183 276 33 160