QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#562524#7743. Grand FinaleReanapWA 85ms99004kbC++142.8kb2024-09-13 18:10:172024-09-13 18:10:18

Judging History

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

  • [2024-09-13 18:10:18]
  • 评测
  • 测评结果:WA
  • 用时:85ms
  • 内存:99004kb
  • [2024-09-13 18:10:17]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define inf (ll)1e9
#define pii pair <ll, ll>
#define fr first
#define se second
const ll mod = 1e9 + 7;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 18, stdin), p1 == p2) ? EOF : *p1++)
inline ll read() {
	ll x = 0, f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9') f = ((ch == '-') ? -1 : f), ch = getchar();
	while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	return x * f;
}
inline void write(ll x) {
	if(x < 0) x = -x, putchar('-');
	if(x >= 10) write(x / 10);
	putchar(x % 10 + '0');
}
inline ll quickmod(ll x, ll y) {
	ll Ans = 1;
	while(y) {
		if(y & 1) Ans = (1ll * Ans * x) % mod;
		x = (1ll * x * x) % mod;
		y >>= 1;
	}
	return Ans;
}
inline void Add(ll &x, ll y) {
	x += y;
	if(x >= mod) x -= mod;
}
inline void Dec(ll &x, ll y) {
	x -= y;
	if(x < 0) x += mod;
}
inline ll add(ll x, ll y) {
	x += y;
	if(x >= mod) x -= mod;
	return x;
}
inline ll dec(ll x, ll y) {
	x -= y;
	if(x < 0) x += mod;
	return x;
}
ll n, m;
char ch1[100005], ch2[100005];
ll f[2505][5005];
inline ll N(char c) {
	if(c == 'Q') return 1;
	if(c == 'B') return 2;
	return 0;
}
int main() {
//	freopen("1.in", "r", stdin);
//	freopen("std.out", "w", stdout);
	ll T = read();
	while(T--) {
		n = read(), m = read();
		for(ll i = 0; i <= m + 1; i++) for(ll j = 0; j <= n + m + 3; j++) f[i][j] = inf;
		scanf("%s", ch1 + 1);
		scanf("%s", ch2 + 1);
		for(ll i = 0; i <= n + m; i++) f[m+1][i] = 0;
		for(ll i = m; i >= 1; i--) {
			for(ll j = 0; j <= n + m; j++) {
				if(j) {
					if(i == m) f[i][j] = 0;
					else {
						f[i][j] = min(f[i][j], max(0ll, f[i+2][j-1+(N(ch2[i])==2)] - (N(ch2[i]) == 1)));
					}
				}
				f[i][j] = min(f[i][j], f[i+1][j+(N(ch2[i])==2)] - (N(ch2[i]) == 1) + 1);
			}
		}
//		printf("??? %lld\n", f[3][1]);
		ll n1 = 0, n2 = 0;
		for(ll i = 1; i <= n; i++) n1 += (N(ch1[i]) == 1), n2 += (N(ch1[i]) == 2);
		ll lst = n - n1 - n2;
		ll ans = inf;
		ll pl = 1, ex = 0;
		while(pl <= m) {
//			printf("??? %lld %lld %lld %lld [%lld]\n", n1, n2, pl, f[pl][n2], ex);
			if(f[pl][n2] <= n1) ans = min(ans, n1 + n2 + ex);
			if(n2) {
				n2--;
				n1 += (N(ch2[pl]) == 1), n2 += (N(ch2[pl]) == 2), ex += (N(ch2[pl]) == 0), pl++;
				if(pl <= m) n1 += (N(ch2[pl]) == 1), n2 += (N(ch2[pl]) == 2), ex += (N(ch2[pl]) == 0), pl++;
			}
			else if(n1) {
				n1--;
				n1 += (N(ch2[pl]) == 1), n2 += (N(ch2[pl]) == 2), ex += (N(ch2[pl]) == 0), pl++;
			}
			else break;
		}
		if(f[pl][n2] <= n1) ans = min(ans, n1 + n2 + ex);
		ans += lst;
		if(ans > n + m) puts("IMPOSSIBLE");
		else write(ans), putchar('\n');
	}
	return 0;
}
/*
1
2 6
BG
BQWBWW
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3904kb

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: 3852kb

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:

3
3

result:

ok 2 lines

Test #3:

score: -100
Wrong Answer
time: 85ms
memory: 99004kb

input:

13
184 887
WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG
QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...

output:

184
372
314
161
118
534
IMPOSSIBLE
631
183
275
33
160
643

result:

wrong answer 3rd lines differ - expected: '316', found: '314'