QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#615550 | #7743. Grand Finale | USTC_fish_touching_team | WA | 30ms | 73804kb | C++14 | 1.9kb | 2024-10-05 19:16:18 | 2024-10-05 19:16:20 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define Re register int
const int N = 5010, Inf = 1000000;
int T, n, m, ans, cnt[5], ct[5], ch[200], a[N], d[5][N], f[N][N], g[N][N];
char sn[N], sm[N];
int main()
{
scanf("%d", &T);
ch['W'] = 0, ch['Q'] = 1, ch['B'] = 2, ch['G'] = 3;
while (T--)
{
scanf("%d%d", &n, &m);
scanf("%s", sn + 1);
scanf("%s", sm + 1);
cnt[1] = cnt[2] = 0;
for (Re i = 1; i <= n; ++i) a[i] = ch[sn[i]];
for (Re i = 1; i <= m; ++i) a[n + i] = ch[sm[i]];
for (Re i = 1; i <= n + m; ++i)
if (a[i] == 1) d[1][++cnt[1]] = i;
else if (a[i] == 2) d[2][++cnt[2]] = i;
//for (Re i = 1; i <= cnt[2]; ++i) printf("d[2][%d] = %d\n", i, d[2][i]);
//printf("!!%d %d\n", cnt[1], cnt[2]);
for (Re i = 0; i <= cnt[1]; ++i)
for (Re j = 0; j <= cnt[2]; ++j)
{
f[i][j] = n;
if (i) f[i][j] = max(f[i][j], f[i - 1][j] + ((f[i - 1][j] >= d[1][i]) ? 1 : 0));
if (j) f[i][j] = max(f[i][j], f[i][j - 1] + ((f[i][j - 1] >= d[2][j]) ? 2 : 0));
//printf("+%d %d %d\n", i, j, f[i][j]);
}
if (f[cnt[1]][cnt[2]] < n + m)
{
puts("IMPOSSIBLE");
continue;
}
ct[1] = cnt[1], ct[2] = cnt[2], ans = n + m;
for (Re i = 0; i <= cnt[2]; ++i) g[n + m + 1][i] = g[n + m + 2][i] = 0;
for (Re i = n + m; i > n; --i)
{
if (a[i] == 1) --ct[1];
else if (a[i] == 2) --ct[2];
for (Re j = 0; j <= ct[2]; ++j)
{
g[i][j] = Inf;
if (j) g[i][j] = g[i + 2][j - 1 + (a[i] == 2)];
g[i][j] = min(g[i][j], max(1, g[i + 1][j + (a[i] == 2)] + (a[i] != 1)));
if (i == n + 1 && j == ct[2] && n == 184) printf("%d!%d!%d\n", g[i][j], ct[1], ct[2]);
if (g[i][j] > ct[1]) continue;
int u = i - 1 - n - 2 * (ct[2] - j);
if (u < 0 || u > ct[1] || ct[1] - u < g[i][j]) continue;
int v = n + ct[2] - j;
if (v >= ans) continue;
if (f[u][ct[2] - j] == i - 1) ans = v;
}
}
printf("%d\n", ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5920kb
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: 5972kb
input:
2 3 8 QBG BBBWBWWW 3 8 QBG BBBWBWWW
output:
3 3
result:
ok 2 lines
Test #3:
score: -100
Wrong Answer
time: 30ms
memory: 73804kb
input:
13 184 887 WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...
output:
108!70!54 240 508 653 161 214 723 IMPOSSIBLE 721 351 406 34 160 754
result:
wrong answer 1st lines differ - expected: '184', found: '108!70!54'