QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#617712#7743. Grand FinaleREN5511TL 0ms3832kbC++144.2kb2024-10-06 16:44:402024-10-06 16:44:41

Judging History

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

  • [2024-10-06 16:44:41]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3832kb
  • [2024-10-06 16:44:40]
  • 提交

answer

#include <iostream>
#include <unordered_map>
#include <set>
using namespace std;
typedef long long ll;
int n, m;
int iniq[2500 + 100] = {0}, inib[2500 + 100] = { 0 };
char hand[2500 + 100], library[2500 + 100];
set<pair<int, pair<int, int>>>mp[2500 + 100];
//抽一 抽二 伤口
bool check(int mid) {
    for (int i = 0; i <= m; i++) {
        mp[i].clear();
    }
    int q=0, b=0, w=0;
    for (int i = 1; i <= n; i++) {
        if (hand[i] == 'Q') {
            q++;
        }
        else if (hand[i] == 'B') {
            b++;
        }
        else {
            w++;
        }
    }
    mp[0].insert({q, { b, w }});
    for (int i = 1; i <= m; i++) {
        if (mp[m].size()) {
            return 1;
        }
        for (auto h : mp[i - 1]) {
            int d1 = h.first, d2 = h.second.first, w = h.second.second;
            if (d1 > 0) {
                d1--;
                if (library[i] == 'Q') {
                    d1++;
                }
                else if (library[i] == 'B') {
                    d2++;
                }
                else {
                    w++;
                }
                mp[i].insert({ d1, { d2, w } });
                if (iniq[i + d1] - iniq[i] + 2 * (inib[i + d1] - inib[i]) + d1 + d2 * 2 + i > m) {
                    return 1;
                }
            }
        }
        if (i > 1) {
            for (auto h : mp[i - 2]) {
                int d1 = h.first, d2 = h.second.first, w = h.second.second;
                if (d2 > 0) {
                    d2--;
                    if (d1 + d2+1 + w < mid) {
                        if (library[i-1] == 'Q') {
                            d1++;
                        }
                        else if (library[i-1] == 'B') {
                            d2++;
                        }
                        else {
                            w++;
                        }
                        if (library[i] == 'Q') {
                            d1++;
                        }
                        else if (library[i] == 'B') {
                            d2++;
                        }
                        else {
                            w++;
                        }
                        mp[i].insert({ d1, { d2, w } });
                        if (iniq[i + d1] - iniq[i] + 2 * (inib[i + d1] - inib[i]) + d1 + d2 * 2 + i > m) {
                            return 1;
                        }
                    }
                    else {
                        if (library[i-1] == 'Q') {
                            d1++;
                        }
                        else if (library[i-1] == 'B') {
                            d2++;
                        }
                        else {
                            w++;
                        }
                        mp[i].insert({ d1, { d2, w } });
                        if (iniq[i+d1]-iniq[i]+ 2*(inib[i + d1] - inib[i]) +d1 + d2 * 2 + i > m) {
                            return 1;
                        }
                    }
                }
            }
        }

    }
    if (mp[m].size())return 1;
    return 0;
}
int findans(int l, int r) {
    int ans = -1;
    while (l <= r) {
        int mid = (l + r) / 2;
        if (check(mid)) {
            ans = mid;
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    return ans;
}
void solve() {
    cin >> n >> m;
    fill(inib, inib + m + 1, 0);
    fill(iniq, iniq + m + 1, 0);
    for (int i = 1; i <= n; i++) {
        cin >> hand[i];
    }
    for (int i = 1; i <= m; i++) {
        cin >> library[i];
        iniq[i] += iniq[i - 1];
        inib[i] += inib[i - 1];
        if (library[i] == 'Q') {
            iniq[i]++;
        }
        else if (library[i] == 'B') {
            inib[i]++;
        }
    }

    int ans = findans(n, m);
    if (ans == -1) {
        cout << "IMPOSSIBLE" << "\n";
    }
    else {
        cout << ans << "\n";
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

详细

Test #1:

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

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

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:

3
3

result:

ok 2 lines

Test #3:

score: -100
Time Limit Exceeded

input:

13
184 887
WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG
QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...

output:


result: