QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#633118#9454. String of CCPCucup-team3519#AC ✓310ms4640kbC++173.2kb2024-10-12 14:35:472024-10-12 14:35:47

Judging History

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

  • [2024-10-12 14:35:47]
  • 评测
  • 测评结果:AC
  • 用时:310ms
  • 内存:4640kb
  • [2024-10-12 14:35:47]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define V vector
#define pb push_back

const int N = 2e5 + 10;
int dp[3][3][3][4];
int n_dp[3][3][3][4];
//0 -> nothing 1 -> C P -> 2

//0 0 0 0 1
//? ? ? ? n + 1
void solve() {
    int n; cin >> n;
    string s; cin >> s;
    s = " " + s;

    V<int> a(n + 1);
    for(int i = 1; i <= n; i++) a[i] = s[i] == 'C' ? 1 : 2;

    auto clear_dp = [&]() -> void {
        for(int i = 0; i <= 2; i++) 
        for(int j = 0; j <= 2; j++) 
        for(int k = 0; k <= 2; k++) 
        for(int p = 0; p <= 3; p++) 
            dp[i][j][k][p] = -1e6;
    };
    auto clear_n_dp = [&]() -> void {
        for(int i = 0; i <= 2; i++) 
        for(int j = 0; j <= 2; j++) 
        for(int k = 0; k <= 2; k++) 
        for(int p = 0; p <= 3; p++) 
            n_dp[i][j][k][p] = -1e6;
    };

    clear_dp();
    dp[0][0][0][0] = 0;

    auto upd = [&](int &n_i, int &n_j, int &n_k, int t) -> bool {
        int ret = 0;
        if(n_i == 1 && n_j == 1 && n_k == 2 && t == 1) ret = 1;
        n_i = n_j, n_j = n_k, n_k = t;
        return ret;
    };

    auto chkmx = [&](int &x, int mx) {
        if(mx > x) {
            x = mx;
            return 1;
        }
        return 0;
    };

    for(int q = 1; q <= n + 1; q++) {
        clear_n_dp();
        for(int p = 0; p <= 3; p++)
        for(int i = 0; i <= 2; i++) 
        for(int j = 0; j <= 2; j++) 
        for(int k = 0; k <= 2; k++) {
            {//take it 
                int n_i = i, n_j = j, n_k = k;
                int val;
                if(q != n + 1){
                    val = dp[i][j][k][p] + upd(n_i, n_j, n_k, a[q]);
                } else val = dp[i][j][k][p];
                // if(q == 7 && dp[i][j][k][p][q] >= 0) {
                //     // cout << "i, j, k, n_i, j, k" << i << " " << j << " " << k << " " << n_i << " " << n_j << " " << n_k << endl;
                // }
                chkmx(n_dp[n_i][n_j][n_k][p], val);
            }
            if(p == 3) continue;
            {//buy C
                int n_i = i, n_j = j, n_k = k;
                int val = dp[i][j][k][p] + upd(n_i, n_j, n_k, 1) - p;
                chkmx(dp[n_i][n_j][n_k][p + 1], val);
            }
            {//buy P
                int n_i = i, n_j = j, n_k = k;
                int val = dp[i][j][k][p] + upd(n_i, n_j, n_k, 2) - p;
                chkmx(dp[n_i][n_j][n_k][p + 1], val);
            }
        }
        swap(n_dp, dp);
    } 

    int ans = 0;

    for(int i = 0; i <= 2; i++) 
    for(int j = 0; j <= 2; j++) 
    for(int k = 0; k <= 2; k++) 
    for(int p = 0; p <= 3; p++) 
        ans = max(ans, dp[i][j][k][p]);
    // for(int i = 0; i <= 2; i++) 
    // for(int j = 0; j <= 2; j++) 
    // for(int k = 0; k <= 2; k++) 
    // for(int p = 0; p <= 3; p++) 
    // for(int q = 1; q <= n + 1; q++) {
    //     if(dp[i][j][k][p][q] >= 0) {
    //         // cout << i << " " << j << " " << k << " " << p << " " << q << " " << dp[i][j][k][p][q] << endl;
    //     }
    // }

    // cout << dp[1][2][1][0][n + 1] << endl;

    cout << ans << endl;
}

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

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
3
CCC
5
CCCCP
4
CPCP

output:

1
1
1

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 310ms
memory: 4640kb

input:

20003
5
PCCPC
10
CPPPPCPCPC
4
CPPC
11
CCPPCPPPCCP
17
PPPPCPCCCCCPCCCCC
10
PPCCPCPPCP
9
CPCCCCPPC
11
PCPPPPCCPPP
15
CPCPPPPCCPCPCCC
11
PCCPPCCPCPP
9
PCPCCPPCP
10
CCPCPPPCPP
14
CCCCPPPCPCPCPP
2
CC
12
CCPCPPPPPCPP
6
CPPPPP
12
PCCPCCCCCPCC
16
CPCCPCCPPCCCCPPC
7
CPPPCPC
16
PPPPPCCPCPCPCPPC
13
PPPCPCCCCPP...

output:

1
1
0
1
2
1
1
1
2
2
1
1
1
0
1
0
3
2
1
2
1
2
2
0
1
2
3
1
1
3
1
2
2
1
0
0
0
3
1
0
0
1
1
2
0
1
1
0
1
2
0
1
0
1
0
3
1
1
0
2
1
3
2
2
0
2
2
0
0
2
1
1
3
3
1
3
1
2
0
1
1
0
1
2
2
1
1
2
1
3
1
1
3
1
2
2
0
1
0
3
0
1
1
2
2
0
2
1
1
2
2
0
3
1
1
1
1
2
1
2
0
1
1
0
3
0
3
1
1
0
0
1
0
3
0
1
1
1
1
2
2
1
1
0
0
1
2
0
1
2
...

result:

ok 20003 lines

Extra Test:

score: 0
Extra Test Passed