QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#243818#7743. Grand FinaleHpSuda#WA 53ms26604kbC++173.2kb2023-11-08 17:46:232023-11-08 17:46:24

Judging History

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

  • [2023-11-08 17:46:24]
  • 评测
  • 测评结果:WA
  • 用时:53ms
  • 内存:26604kb
  • [2023-11-08 17:46:23]
  • 提交

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);
                    }
                }
            }
        }
    }
//    for (int i = 0; i <= m; ++i) {
//        for (int j = 0; j <= m; ++j) {
//            cout << f[i][j] << " ";
//        }
//        cout << "\n";
//    }
//    cout << "\n";
    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 {
        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;
        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();
    }
}

詳細信息

Test #1:

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

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

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:

3
3

result:

ok 2 lines

Test #3:

score: -100
Wrong Answer
time: 53ms
memory: 26604kb

input:

13
184 887
WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG
QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...

output:

IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
161
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE
160
IMPOSSIBLE

result:

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