QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#633714#7743. Grand FinalecomplexorWA 79ms40068kbC++203.1kb2024-10-12 16:06:242024-10-12 16:06:24

Judging History

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

  • [2024-10-12 16:06:24]
  • 评测
  • 测评结果:WA
  • 用时:79ms
  • 内存:40068kb
  • [2024-10-12 16:06:24]
  • 提交

answer

#include <bits/stdc++.h>
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef __int128 LLL;
typedef std::pair<int, int> pii;
typedef std::pair<LL, int> pli;
typedef std::pair<LL, LL> pll;
#define fi first
#define se second
#define MP std::make_pair
LL read()
{
	LL s = 0; int f = 1;
	char c = getchar();
	while (!(c >= '0' && c <= '9'))
		f ^= (c == '-'), c = getchar();
	while (c >= '0' && c <= '9')
		s = s * 10 + (c ^ 48), c = getchar();
	return f ? s : -s;
} 
template<typename T> void write(T x, char end = ' ')
{
	static int d[100], cur = 0;
	do{ d[++cur] = x % 10; } while (x /= 10);
	while (cur) putchar('0' + d[cur--]);
	putchar(end);
}
const int MAXN = 2505, inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fll;
const int MOD = 1e9 + 7;
auto fplus = [](int x, int y){ return x + y >= MOD ? x + y - MOD : x + y; };
auto fminus = [](int x, int y){ return x >= y ? x - y : x + MOD - y; };
auto Fplus = [](int &x, int y){ return x = fplus(x, y); };
auto Fminus = [](int &x, int y){ return x = fminus(x, y); };
template<typename T> T& Fmax(T& x, T y){ return x = x < y ? y : x; };
template<typename T> T& Fmin(T& x, T y){ return x = x < y ? x : y; };
int fpow(int x, int y = MOD - 2)
{
	int res = 1;
	for (; y; y >>= 1, x = (LL)x * x % MOD)
		if (y & 1) res = (LL)res * x % MOD;
	return res;
}
int n, m, c1, c2, f[MAXN][MAXN];
bool g[MAXN][MAXN << 1];
char s[MAXN], t[MAXN];
void mian()
{
	n = read(), m = read();
	scanf("%s %s", s + 1, t + 1);
	c1 = c2 = 0;
	for (int i = 1; i <= n; i++)
		c1 += (s[i] == 'Q'), c2 += (s[i] == 'B');
	for (int i = 0; i <= m + 1; i++)
		memset(f[i], 0x3f, sizeof f[i]), memset(g[i], false, sizeof g[i]);
	for (int i = 0; i <= m + c2; i++) f[m + 1][i] = 0;
	for (int i = m; i; i--)
		for (int j = 0; j < i + c2; j++)
		{
			Fmin(f[i][j], f[i + 1][j + (t[i] == 'B')] + 1 - (t[i] == 'Q'));
			if (j) Fmin(f[i][j], std::max(0, f[std::min(m + 1, i + 2)][j + (t[i] == 'B') - 1] - (t[i] == 'Q')));
//			printf("%d %d: %d\n", i, j, f[i][j]);
		}
	g[1][c2] = true;
	for (int i = 1, c = c1, cc = c2; i <= m; i++)
	{
		for (int j = 0; j < i + c2; j++)
			if (g[i][j])
			{
				if ((cc - j) * 2 > i - 1) continue;
				int jj = c1 - (i - 1 - 2 * (cc - j));
//				printf("%d: %d %d\n", i, j, jj);
				if (jj < 0) continue;
				if (jj) g[i + 1][j + (t[i] == 'B')] = true;
				if (j) 
				{
					if (i < m) g[i + 2][j - 1 + (t[i] == 'B') + (t[i + 1] == 'B')] = true;
					else g[m + 1][j - 1 + (t[i] == 'B')] = true;
				}
			}
		c += (t[i] == 'Q'), cc += (t[i] == 'B');
	}
	bool vld = false;
	for (int i = 0; i <= m + c2; i++) vld |= g[m + 1][i];
	if (!vld) return printf("IMPOSSIBLE\n"), void();
	for (int ans = n; ; ans++)
		for (int i = 1, c = c1, cc = c2; i <= m; c += (t[i] == 'Q'), cc += (t[i] == 'B'), i++)
		{
			if (i - 1 - 2 * (ans - n) < 0) continue;
			int x = cc - (ans - n), y = c - (i - 1 - 2 * (ans - n));
//			printf("%d: %d %d %d %d %d\n", ans, i, x, y, cc, c);
			if (x >= 0 && y >= 0 && g[i][x] && y >= f[i][x]) 
				return printf("%d\n", ans), void();
		}
}
int main()
{
	for (int T = read(); T--; )
	{
		mian();
		
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5908kb

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

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:

3
3

result:

ok 2 lines

Test #3:

score: -100
Wrong Answer
time: 79ms
memory: 40068kb

input:

13
184 887
WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG
QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...

output:

IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
33
160
IMPOSSIBLE

result:

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