QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#345517#7743. Grand FinaleSorting#WA 1ms3648kbC++203.4kb2024-03-07 04:15:582024-03-07 04:15:58

Judging History

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

  • [2024-03-07 04:15:58]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3648kb
  • [2024-03-07 04:15:58]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

using pii = pair<int, int>;
using vi = vector<int>;
using vpii = vector<pii>;
using vvi = vector<vi>;

const int N = 5005;

int dp1[N][N];
int dp2[N][N];

const int INF = 1e9 + 7;

void solve() {
    int n, m;
    cin >> n >> m;
    string hand, draw;
    cin >> hand >> draw;

    for(int i = 0; i <= m + 5; i++) {
        for(int j = 0; j <= n+m + 5; j++) {
            dp1[i][j] = -INF;
            dp2[i][j] = INF;
        }
    }

    int hand_q = 0, hand_b = 0, hand_w = 0;
    for(char c : hand) {
        if(c == 'Q') {
            hand_q++;
        } else if(c == 'B') {
            hand_b++;
        } else {
            hand_w++;
        }
    }
    vi draw_q(m+1), draw_b(m+1), draw_w(m+1);
    for(int i = 0; i < m; i++) {
        draw_q[i+1] = draw_q[i];
        draw_b[i+1] = draw_b[i];
        draw_w[i+1] = draw_w[i];
        if(draw[i] == 'Q') {
            draw_q[i+1]++;
        } else if(draw[i] == 'B') {
            draw_b[i+1]++;
        } else {
            draw_w[i+1]++;
        }
    }

    dp1[0][hand_q] = hand_b;
    for(int i = 0; i < m; i++) {
        for(int q = 0; q <= (n+m); q++) {
            if(dp1[i][q] == -INF) continue;
            int dq = draw[i] == 'Q';
            int db = draw[i] == 'B';

            // play a Q
            if(q > 0) {
                dp1[i+1][q-1+dq] = max(dp1[i+1][q-1+dq], dp1[i][q] + db);
            }
            // play a B
            if(dp1[i][q] > 0) {
                if(i + 1 < m) {
                    dq += draw[i+1] == 'Q';
                    db += draw[i+1] == 'B';
                }
                dp1[i+1][q+dq] = max(dp1[i+1][q+dq], dp1[i][q] + db - 1);
            }
        }
    }

    for(int q = 0; q <= n+m; q++) {
        dp2[m][q] = 0;
    }
    for(int i = m; i > 0; i--) {
        for(int q = 0; q <= (n+m); q++) {
            if(dp2[i][q] == INF) continue;
            
            int b = dp2[i][q];

            // played a Q
            {
                int q0 = q + 1 - (draw[i-1] == 'Q');
                int b0 = b - (draw[i-1] == 'B');
                if(q0 > 0) {
                    dp2[i-1][q0] = min(dp2[i-1][q0], b0);
                }
            }

            // played a B
            if(i >= 2) {
                int q0 = q - (draw[i-2] == 'Q');
                int b0 = b + 1 - (draw[i-2] == 'B');
                if(b0 > 0) {
                    dp2[i-1][q0] = min(dp2[i-1][q0], b0);
                }
            }
        }
    }


    auto ok = [&](int k) {
        for(int s = 0; s <= m; s++) {
            int b0 = k - n;
            int q0 = s - 2 * b0;
        
            if(dp1[s][q0] >= b0 && dp2[s][q0] <= b0) {
                return true;
            }
        }
        
        return false;
    };

    for(int k = 0; k <= (n+m); k++) {
        if(ok(k)) {
            cout << k+1 << endl;
            return;
        }
    }
    cout << "IMPOSSIBLE" << endl;




    // int lo = 0, hi = (n+m);
    // while(lo + 1 < hi) {
    //     int k = (lo + hi) / 2;

    //     if(ok(k+1)) {
    //         hi = k;
    //     } else {
    //         lo = k;
    //     }

    // }

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int tc;
    cin >> tc;
    while(tc--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
2 6
BG
BQWBWW
4 6
GQBW
WWWWQB

output:

3
IMPOSSIBLE

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 3648kb

input:

2
3 8
QBG
BBBWBWWW
3 8
QBG
BBBWBWWW

output:

5
5

result:

wrong answer 1st lines differ - expected: '3', found: '5'