QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#600454#7743. Grand FinalezhangbojuWA 0ms5664kbC++142.3kb2024-09-29 16:41:092024-09-29 16:41:10

Judging History

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

  • [2024-09-29 16:41:10]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:5664kb
  • [2024-09-29 16:41:09]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
constexpr int N = 2505;
int f[N][N];
bool g[N][N];
int n, m;
string str;
int a[N];
void solve() {
	cin >> n >> m;
	cin >> str;
	int cnt1 = 0, cnt2 = 0;
	for (int i = 0; i < n; i++) {
		if (str[i] == 'Q') ++cnt1;
		else if (str[i] == 'B') ++cnt2;
	}
	cin >> str;
	for (int i = 0; i < m; i++) {
		if (str[i] == 'B') a[i + 1] = 2;
		else if (str[i] == 'Q') a[i + 1] = 1;
		else if (str[i] == 'W') a[i + 1] = 0;
	}
	for (int i = 0; i <= m; i++) {
		fill(f[i], f[i] + m + 1, 0x3f3f3f3f);
		fill(g[i], g[i] + m + 1, 0);
	}
	for (int i = 0; i <= m + 1; i++)
		f[m][i] = f[m + 1][i] = 0;
	for (int i = m - 1; i >= 0; i--) {
		for (int j = 0; j <= m; j++) {
			if (j * 2 >= m - i) {
				f[i][j] = 0;
				continue;
			}
			if (a[i + 1] == 0) {
				f[i][j] = min(f[i][j], f[i + 1][j] + 1);
			}
			else if (a[i + 1] == 1) {
				f[i][j] = min(f[i][j], max(1, f[i + 1][j]));
			}
			else {
				f[i][j] = min(f[i][j], f[i + 1][j + 1] + 1);
			}
			
			if (!j) continue;

			if (a[i + 1] == 0) {
				f[i][j] = min(f[i][j], f[i + 2][j - 1]);
			}
			else if (a[i + 1] == 1) {
				f[i][j] = min(f[i][j], max(0, f[i + 2][j - 1] - 1));
			}
			else {
				f[i][j] = min(f[i][j], f[i + 2][j]);
			}
		}
	}

	g[0][cnt2] = 1;
	int sum1 = cnt1, sum2 = cnt2;
	for (int i = 0; i < m; i++) {

		for (int j = 0; j <= sum2; j++) {
			if (!g[i][j]) continue;
			int now1 = sum1 - (i - 2 * (sum2 - j));
			if (now1 < 0) continue;
			if (now1 > 0) {
				if (a[i + 1] == 2) g[i + 1][j + 1] = 1;
				else g[i + 1][j] = 1;
			}

			if (j && i + 2 <= m) {
				int cnt = (a[i + 1] == 2) + (a[i + 2] == 2);
				g[i + 2][j - 1 + cnt] = 1;
			} 
		}

		if (a[i + 1] == 1) sum1++;
		else if (a[i + 1] == 2) sum2++;
	}

	for (int k = n; k <= n + m; k++) {
		int sum1 = cnt1, sum2 = cnt2;
		for (int t = 0; t <= m; t++) {
			int tot1 = sum1 - t + k - n;
			int tot2 = sum2 - k + n;
			if (tot1 >= 0 && tot2 >= 0 && f[t][tot2] <= tot1 && g[t][tot2]) {
				cout << k << "\n";
				return;
			}
			if (t < m && a[t + 1] == 1) sum1++;
			else if (t < m && a[t + 1] == 2) sum2++;
		}
	}
	cout << "IMPOSSIBLE\n";
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int T;cin >> T;
	while (T--) {
		solve();
	}
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 5664kb

input:

2
2 6
BG
BQWBWW
4 6
GQBW
WWWWQB

output:

IMPOSSIBLE
IMPOSSIBLE

result:

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