QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#243824#7743. Grand FinaleHpSuda#WA 46ms26828kbC++173.1kb2023-11-08 17:52:312023-11-08 17:52:32

Judging History

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

  • [2023-11-08 17:52:32]
  • 评测
  • 测评结果:WA
  • 用时:46ms
  • 内存:26828kb
  • [2023-11-08 17:52:31]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
using ll = long long;

void solve() {
    int n, m;
    cin >> n >> m;
    string a, b;
    cin >> a >> b;
    vector<vector<int>> f(m + 2, vector<int>(m + 1, n + m + 1));
    for (int i = 0; i <= m; ++i) {
        f[m][i] = f[m+1][i] = 0;
    }
    for (int i = m - 1; i >= 0; i--) {
        int cur = 0;
        if (b[i] == 'B') cur = 2;
        else if (b[i] == 'Q') cur = 1;
        for (int j = 0; j <= m; j++) {
            if (!j) {
                if (cur == 1) {
                    f[i][j] = max(1, f[i + 1][j]);
                }
                else if (cur == 2) {
                    f[i][j] = max(1, f[i + 1][j + 1] + 1);
                }
                else {
                    f[i][j] = max(1, f[i+1][j] + 1);
                }
            }
            else {
                if (j * 2 >= m - i) {
                    f[i][j] = 0;
                }
                else {
                    if (cur == 1) {
                        f[i][j] = max(0, f[i + 2][j - 1]);
                        f[i][j] = min(f[i][j], max(1, f[i + 1][j]));
                    }
                    else if (cur == 2) {
                        f[i][j] = f[i + 2][j];
                        f[i][j] = min(f[i][j], f[i + 1][j + 1] + 1);
                    }
                    else {
                        f[i][j] = f[i + 2][j - 1];
                        f[i][j] = min(f[i][j], f[i + 1][j] + 1);
                    }
                }
            }
        }
    }
    int c1 = 0, c2 = 0;
    int sum = n;
    for (int i = 0; i < n; i++) {
        if (a[i] == 'Q') ++c1;
        else if (a[i] == 'B') ++c2;
    }
    auto check = [&](int x) -> bool {
        sum = n;
        int cur = 0;
        int s1 = c1, s2 = c2;
        while (cur < m && sum < x) {
            if (s2) {
                s2--;
                if (b[cur] == 'B') {
                    s2++;
                }
                else if (b[cur] == 'Q') {
                    s1++;
                }
                sum++;
                cur++;
                if (cur == m) break;
                if (b[cur] == 'B') {
                    s2++;
                }
                else if (b[cur] == 'Q') {
                    s1++;
                }
            }
            else if (s1) {
                s1--;
                if (b[cur] == 'B') {
                    s2++;
                }
                else if (b[cur] == 'Q') {
                    s1++;
                }
                cur++;
            }
            else {
                break;
            }
        }
        if (cur == m) return true;
        if (!s1 && !s2) return false;
        if (s2 * 2 + s1 >= m - cur) return true;
        return f[cur][s2] <= s1;
    };
    for (int i = n; i <= n + m; i++) {
        if (check(i)) {
            cout << i << "\n";
            return;
        }
    }
    cout << "IMPOSSIBLE\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:

3
3

result:

ok 2 lines

Test #3:

score: -100
Wrong Answer
time: 46ms
memory: 26828kb

input:

13
184 887
WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG
QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...

output:

256
717
864
161
258
945
IMPOSSIBLE
1023
428
524
35
160
1092

result:

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