QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#593117#7743. Grand FinaleetherealUnicornWA 16ms64204kbC++143.0kb2024-09-27 11:41:342024-09-27 11:41:35

Judging History

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

  • [2024-09-27 11:41:35]
  • 评测
  • 测评结果:WA
  • 用时:16ms
  • 内存:64204kb
  • [2024-09-27 11:41:34]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 2510;
const int INF = 1e9;

int is1[N], is2[N], pre1[N], pre2[N];
bool dp1[N][2 * N];
int dp2[N][2 * N];

void init(int n, int m);
void solve(int n, int m);
void solve_dp1(int n, int m);
void solve_dp2(int n, int m);
bool check(int n, int m, int volume);

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int t, n, m;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        init(n, m);
        solve(n, m);
    }
    return 0;
}

void init(int n, int m)
{
    string card;
    cin >> card;
    pre1[0] = pre2[0] = 0;
    for (auto c : card)
    {
        if (c == 'Q')
            pre1[0]++;
        else if (c == 'B')
            pre2[0]++;
    }
    cin >> card;
    for (int i = 1; i <= m; i++)
    {
        is1[i] = (card[i - 1] == 'Q');
        pre1[i] = pre1[i - 1] + is1[i];
        is2[i] = (card[i - 1] == 'B');
        pre2[i] = pre2[i - 1] + is2[i];
    }
    return;
}

void solve(int n, int m)
{
    solve_dp1(n, m);
    solve_dp2(n, m);
    for (int v = n; v <= n + m; v++)
        if (check(n, m, v))
        {
            cout << v << endl;
            return;
        }
    cout << "IMPOSSIBLE" << endl;
    return;
}

void solve_dp1(int n, int m)
{
    for (int i = 0; i <= m; i++)
        for (int j = 0; j < pre2[i]; j++)
            dp1[i][j] = false;
    dp1[0][pre2[0]] = true;
    for (int i = 0; i < m; i++)
        for (int j = 0; j <= pre2[i]; j++)
        {
            if (!dp1[i][j])
                continue;
            if (pre1[i] - (i - 2 * (pre2[i] - j)) > 0)
                dp1[i + 1][j + is2[i + 1]] = true;
            if (j > 0)
            {
                if (i == m - 1)
                    dp1[i + 1][j - 1 + is2[i + 1]] = true;
                else
                    dp1[i + 2][j - 1 + is2[i + 1] + is2[i + 2]] = true;
            }
        }
    return;
}

void solve_dp2(int n, int m)
{
    for (int j = 0; j <= pre2[m]; j++)
        dp2[m][j] = 0;
    for (int i = m - 1; i >= 0; i--)
    {
        dp2[i][0] = max(1, dp2[i + 1][is2[i + 1]] + 1 - is1[i + 1]);
        for (int j = 1; j <= pre2[i]; j++)
        {
            dp2[i][j] = max(1, dp2[i + 1][j + is2[i + 1]] + 1 - is1[i + 1]);
            dp2[i][j] = min(dp2[i][j], dp2[min(i + 2, m)][j - 1 + is2[i + 1]] - is1[i + 1]);
        }
    }
    return;
}

bool check(int n, int m, int volume)
{
    bool ret;
    int cnt1, cnt2;
    for (int t = 0; t <= m; t++)
    {
        cnt1 = pre1[t] - (t - 2 * (volume - n));
        cnt2 = pre2[t] - (volume - n);
        ret = (cnt1 >= 0) && (cnt2 >= 0) && dp1[t][cnt2] && (dp2[t][cnt2] <= cnt1);
        if (ret)
        {
            // cout << "volume = " << volume << ", t = " << t << endl;
            // cout << "cnt1 = " << cnt1 << ", cnt2 = " << cnt2 << endl;
            // cout << "dp1 = " << dp1[t][cnt2] << ", dp2 = " << dp2[t][cnt2] << endl;
            return true;
        }
    }
    return false;
}

详细

Test #1:

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

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

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:

3
3

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 15ms
memory: 63880kb

input:

13
184 887
WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG
QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...

output:

184
372
316
161
118
534
IMPOSSIBLE
631
183
276
33
160
643

result:

ok 13 lines

Test #4:

score: 0
Accepted
time: 16ms
memory: 64204kb

input:

32
887 278
BQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQQWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQBWW...

output:

887
981
15
18
60
9
108
268
475
17
52
12
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
76
14
182
907
537
19
19
233
10
38
111
143
103
159
14
IMPOSSIBLE
225

result:

ok 32 lines

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 36636kb

input:

120
37 178
BQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBG
BWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQQWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQ
21 184
QQBQBWQQQBWQWQBWWBQQG
QWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBB...

output:

37
24
41
23
46
22
21
24
31
178
23
26
50
53
23
24
25
24
59
43
17
37
35
46
44
42
25
44
33
33
8
14
21
IMPOSSIBLE
36
32
50
79
64
42
34
42
12
27
32
35
44
13
50
IMPOSSIBLE
47
38
37
16
42
38
47
34
37
30
45
61
46
42
42
34
30
32
27
IMPOSSIBLE
25
14
39
31
39
38
50
96
26
36
50
5
IMPOSSIBLE
49
39
25
27
IMPOSSIB...

result:

wrong answer 14th lines differ - expected: 'IMPOSSIBLE', found: '53'