답안 #788539

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
788539 2023-07-20T10:37:53 Z someone Chorus (JOI23_chorus) C++14
61 / 100
868 ms 197956 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 5e3 + 42, INF = 1e12 + 42;

deque<int> imin, a, b;
vector<int> pre, upd[N];
int n, k, cumul[N], fi[N], dp[N][N];

int evalCHT(int x) {
    if(a.empty()) return INF;
    while((int)a.size() >= 2 && a[0] * x + b[0] > a[1] * x + b[1]) {
        a.pop_front();
        b.pop_front();
    }
    return a[0] * x + b[0];
}

void pushCHT(int A, int B) {
    int sz = (int)a.size();
    while(sz >= 2 && (B - b[sz-1]) * (a[sz-2] - a[sz-1]) <= (b[sz-1] - b[sz-2]) * (a[sz-1] - A)) {
        sz--;
        a.pop_back();
        b.pop_back();
    }
    a.push_back(A);
    b.push_back(B);
}

void add(int i, int nbG) {
    if(fi[i] == i)  {
        pushCHT(-i, dp[nbG-1][i] + i * fi[i] - cumul[fi[i]]);
        return;
    }
    upd[fi[i]].push_back(i);
    while(!imin.empty() && dp[nbG-1][imin.back()] > dp[nbG-1][i])
        imin.pop_back();
    imin.push_back(i);
}

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

    int x = 0;
    cin >> n >> k;
    for(int i = 0; i < N; i++)
        fi[i] = N-1;
    pre.clear();
    int nbA = 0;
    for(int i = 0; i < 2*n; i++) {
        char c; cin >> c;
        if(c == 'A') {
            nbA++;
            if(fi[x] == N-1)
                fi[x] = (int)pre.size();
            pre.push_back(x);
        } else {
            x++;
        }
    }
    for(int i = n-1; i > -1; i--)
        if(fi[i] == N-1)
            fi[i] = fi[i+1];
    for(int i = 0; i <= n; i++)
        fi[i] = max(fi[i], i);

    for(int i = 1; i <= n; i++)
        cumul[i] = cumul[i-1] + pre[i-1];
    for(int i = 1; i <= n; i++)
        dp[0][i] = INF;

    for(int nbG = 1; nbG <= k; nbG++) {
        a.clear();
        b.clear();
        imin.clear();
        for(int i = 0; i < N; i++)
            upd[i].clear();

        add(0, nbG);
        for(int i = 1; i <= n; i++) {
            for(int j : upd[i]) {
                pushCHT(-j, dp[nbG-1][j] + j * fi[j] - cumul[fi[j]]);
                if(!imin.empty() && imin[0] == j)
                    imin.pop_front();
            }
            dp[nbG][i] = evalCHT(i) + cumul[i];
            if(!imin.empty())
                dp[nbG][i] = min(dp[nbG][i], dp[nbG-1][imin[0]]);
            add(i, nbG);
        }
    }
    cout << dp[k][n] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 480 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 480 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 3 ms 1220 KB Output is correct
19 Correct 8 ms 2688 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 472 KB Output is correct
22 Correct 9 ms 4432 KB Output is correct
23 Correct 10 ms 4464 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 9 ms 4436 KB Output is correct
26 Correct 8 ms 3796 KB Output is correct
27 Correct 4 ms 1748 KB Output is correct
28 Correct 4 ms 1748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 480 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 3 ms 1220 KB Output is correct
19 Correct 8 ms 2688 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 472 KB Output is correct
22 Correct 9 ms 4432 KB Output is correct
23 Correct 10 ms 4464 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 9 ms 4436 KB Output is correct
26 Correct 8 ms 3796 KB Output is correct
27 Correct 4 ms 1748 KB Output is correct
28 Correct 4 ms 1748 KB Output is correct
29 Correct 287 ms 81380 KB Output is correct
30 Correct 868 ms 171680 KB Output is correct
31 Correct 215 ms 41276 KB Output is correct
32 Correct 1 ms 724 KB Output is correct
33 Correct 1 ms 724 KB Output is correct
34 Correct 605 ms 197956 KB Output is correct
35 Correct 590 ms 197888 KB Output is correct
36 Correct 4 ms 1108 KB Output is correct
37 Correct 243 ms 40168 KB Output is correct
38 Correct 507 ms 163896 KB Output is correct
39 Correct 437 ms 94528 KB Output is correct
40 Correct 235 ms 66396 KB Output is correct
41 Correct 241 ms 66420 KB Output is correct
42 Correct 158 ms 66400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 480 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 3 ms 1220 KB Output is correct
19 Correct 8 ms 2688 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 472 KB Output is correct
22 Correct 9 ms 4432 KB Output is correct
23 Correct 10 ms 4464 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 9 ms 4436 KB Output is correct
26 Correct 8 ms 3796 KB Output is correct
27 Correct 4 ms 1748 KB Output is correct
28 Correct 4 ms 1748 KB Output is correct
29 Correct 287 ms 81380 KB Output is correct
30 Correct 868 ms 171680 KB Output is correct
31 Correct 215 ms 41276 KB Output is correct
32 Correct 1 ms 724 KB Output is correct
33 Correct 1 ms 724 KB Output is correct
34 Correct 605 ms 197956 KB Output is correct
35 Correct 590 ms 197888 KB Output is correct
36 Correct 4 ms 1108 KB Output is correct
37 Correct 243 ms 40168 KB Output is correct
38 Correct 507 ms 163896 KB Output is correct
39 Correct 437 ms 94528 KB Output is correct
40 Correct 235 ms 66396 KB Output is correct
41 Correct 241 ms 66420 KB Output is correct
42 Correct 158 ms 66400 KB Output is correct
43 Runtime error 5 ms 1620 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 480 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 0 ms 468 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 3 ms 1220 KB Output is correct
19 Correct 8 ms 2688 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 472 KB Output is correct
22 Correct 9 ms 4432 KB Output is correct
23 Correct 10 ms 4464 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 9 ms 4436 KB Output is correct
26 Correct 8 ms 3796 KB Output is correct
27 Correct 4 ms 1748 KB Output is correct
28 Correct 4 ms 1748 KB Output is correct
29 Correct 287 ms 81380 KB Output is correct
30 Correct 868 ms 171680 KB Output is correct
31 Correct 215 ms 41276 KB Output is correct
32 Correct 1 ms 724 KB Output is correct
33 Correct 1 ms 724 KB Output is correct
34 Correct 605 ms 197956 KB Output is correct
35 Correct 590 ms 197888 KB Output is correct
36 Correct 4 ms 1108 KB Output is correct
37 Correct 243 ms 40168 KB Output is correct
38 Correct 507 ms 163896 KB Output is correct
39 Correct 437 ms 94528 KB Output is correct
40 Correct 235 ms 66396 KB Output is correct
41 Correct 241 ms 66420 KB Output is correct
42 Correct 158 ms 66400 KB Output is correct
43 Runtime error 5 ms 1620 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -