QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#588091#7743. Grand Finaleucup-team3519RE 1ms5608kbC++202.9kb2024-09-25 01:03:102024-09-25 01:03:10

Judging History

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

  • [2024-09-25 01:03:10]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:5608kb
  • [2024-09-25 01:03:10]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define V vector
#define all0(x) (x).begin(),(x).end()
#define all1(x) (x).begin()+1,(x).end()
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define cin std::cin
#define cout std::cout
typedef long long LL;
typedef pair<int, int> pi;
typedef pair<LL, LL> pl;

//const int MN = 2e5 + 20;
const int INF = 2e9 + 1000;//INF
const LL INFLL = 8e18 + 1000;//INF long long 
mt19937 mrand(chrono::steady_clock().now().time_since_epoch().count());
//模板区域~~~~~~~

//模板结束~~~~~~~
const int MN = 5010;
int dp[MN][MN];
void solve() {
    int n, m; cin >> n >> m;
    string s, t; cin >> s >> t;
    int l = n - 1, r = n + m + 1;

    auto add = [&](int &c1, int &c2, int &w, char c) -> void {
        if(c == 'Q') c1++; else if(c == 'B') c2++; else w++;
    };
    auto fine = [&](int k) -> bool {
        // cout << " k : " << k << endl;
        int c1 = 0, c2 = 0, w = 0;
        for(auto c : s) add(c1, c2, w, c);
        int now = 0;
        while(c1 + c2 + w < k && (c1 || c2)) {
            if(c2) c2--, add(c1, c2, w, t[now]), add(c1, c2, w, t[now + 1]), now = now + 2;
            else c1--, add(c1, c2, w, t[now]), now++;
        }
        if(c1 + c2 + w != k) {
            return 0;
        }
        for(int i = 0; i <= m - now; i++) for(int j = 0; j <= k; j++) dp[i][j] = -1;
        dp[0][c1] = c2;
        for(int i = 0; i < m - now; i++) {
            for(int j = 0; j <= k; j++) {
                if(dp[i][j] == -1) continue;
                if(dp[i][j]) {
                    int to = min(i + 2, m - now);
                    int n_c1 = j, n_c2 = dp[i][j] - 1, tmp;
                    add(n_c1, n_c2, tmp, t[i + now]);
                    dp[to][n_c1] = max(dp[to][n_c1], n_c2);
                }
                if(j) {
                    int to = i + 1;
                    int n_c1 = j - 1, n_c2 = dp[i][j], tmp;
                    add(n_c1, n_c2, tmp, t[i + now]);
                    dp[to][n_c1] = max(dp[to][n_c1], n_c2);
                }
            }
        }
        // cout << "now, c1, c2, w : " << now << " " << c1 << " " << c2 << " " << w << endl;
        // cout << "DP : " << endl;
        // for(int i = 0; i <= m - now; i++) {
        //     for(int j = 0; j <= k; j++) cout << dp[i][j] << " ";
        //     cout << endl;
        // }
        bool ok = 0;
        for(int j = 0; j <= k; j++) if(dp[m - now][j] != -1) ok = 1;
        return ok;
    };
    
    while(l != r - 1) {
        int mid = l + r >> 1;
        if(fine(mid)) r = mid;
        else l = mid;
    }

    if(r == n + m + 1) {
        cout << "IMPOSSIBLE" << endl;
    } else cout << r << endl;

}

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) 
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:

3
3

result:

ok 2 lines

Test #3:

score: -100
Runtime Error

input:

13
184 887
WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG
QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...

output:

IMPOSSIBLE
IMPOSSIBLE
IMPOSSIBLE

result: