QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#343940#7743. Grand Finaleucup-team992#WA 4ms101596kbC++202.5kb2024-03-03 10:07:192024-03-03 10:07:19

Judging History

你现在查看的是最新测评结果

  • [2024-03-03 10:07:19]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:101596kb
  • [2024-03-03 10:07:19]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
ll INF=1000000000;
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+1,0);
		QPS.assign(M+1,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];
		}
		
		ll R = INF;

		for(int i = 0; i <= M; i++) DP0[i].assign(M+1, false);
		for(int i = 0; i <= M; i++) DP1[i].assign(M+N+1,INF);

		DP1[M].assign(M+N+1,0);
		for(int i = M-1; i >= 0; i--) {
			for(int b = 0; b <= N+M; b++) {
				// we are at full hand-size.
				// play a Q from here...
				{
					ll bthen = b;
					if(P[i] == 'B') bthen++;
					ll cost = 0;
					if(P[i] == 'Q') cost = 1;
					if(bthen >= 0) {
						DP1[i][b] = min(DP1[i][b], 1 + max(DP1[i+1][bthen]-cost,0LL));
					}
				}


				// played a B to get here.
				if(b > 0) {
					if(i+2 > M) {
						DP1[i][b] = 0;
					} else {
						ll bthen = b-1;
						if(P[i] == 'B') bthen++;
						ll cost = 0;
						if(P[i] == 'Q') cost=1;
						if(bthen >= 0) {
							DP1[i][b] = min(DP1[i][b], max(DP1[i+2][bthen]-cost,0LL));
						}
					}
				}

				//cout << i << " " << b << " " << DP1[i][b] << endl;
			}
		}


		DP0[0][0] = true;
		for(int i = 1; i <= M; i++) {
			for(int b = 0; (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;
				}
			}
		}

		for(int i = 0; i <= M; i++) for(int b = 0; b <= M; b++) {
			ll qnow = QPS[i] + Q0;
			qnow -= i - 2*b;
			ll bnow = BPS[i]+B0-b;
			if(DP0[i][b] && qnow >= DP1[i][bnow]) R = min(R, N+b);
			if(i == M) R = min(R, N+b);
			if(i >= M-2 && bnow>0) R = min(R, N+b);
		}


		if(R == INF) {
			cout << "IMPOSSIBLE\n"; continue;
		}

		cout << R << endl;
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 101596kb

input:

2
2 6
BG
BQWBWW
4 6
GQBW
WWWWQB

output:

2
4

result:

wrong answer 1st lines differ - expected: '3', found: '2'