Submission #788550

# Submission time Handle Problem Language Result Execution time Memory
788550 2023-07-20T10:47:43 Z someone Chorus (JOI23_chorus) C++14
61 / 100
859 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) {
    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';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 456 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 456 KB Output is correct
9 Correct 1 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 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 456 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 456 KB Output is correct
9 Correct 1 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 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 712 KB Output is correct
18 Correct 3 ms 1228 KB Output is correct
19 Correct 9 ms 2636 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 452 KB Output is correct
22 Correct 8 ms 4504 KB Output is correct
23 Correct 9 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 7 ms 3784 KB Output is correct
27 Correct 4 ms 1748 KB Output is correct
28 Correct 3 ms 1864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 456 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 456 KB Output is correct
9 Correct 1 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 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 712 KB Output is correct
18 Correct 3 ms 1228 KB Output is correct
19 Correct 9 ms 2636 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 452 KB Output is correct
22 Correct 8 ms 4504 KB Output is correct
23 Correct 9 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 7 ms 3784 KB Output is correct
27 Correct 4 ms 1748 KB Output is correct
28 Correct 3 ms 1864 KB Output is correct
29 Correct 280 ms 81384 KB Output is correct
30 Correct 859 ms 171592 KB Output is correct
31 Correct 210 ms 41228 KB Output is correct
32 Correct 1 ms 712 KB Output is correct
33 Correct 1 ms 724 KB Output is correct
34 Correct 599 ms 197956 KB Output is correct
35 Correct 623 ms 197920 KB Output is correct
36 Correct 4 ms 1108 KB Output is correct
37 Correct 230 ms 40140 KB Output is correct
38 Correct 550 ms 163888 KB Output is correct
39 Correct 429 ms 94504 KB Output is correct
40 Correct 240 ms 66424 KB Output is correct
41 Correct 245 ms 66368 KB Output is correct
42 Correct 149 ms 66336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 456 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 456 KB Output is correct
9 Correct 1 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 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 712 KB Output is correct
18 Correct 3 ms 1228 KB Output is correct
19 Correct 9 ms 2636 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 452 KB Output is correct
22 Correct 8 ms 4504 KB Output is correct
23 Correct 9 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 7 ms 3784 KB Output is correct
27 Correct 4 ms 1748 KB Output is correct
28 Correct 3 ms 1864 KB Output is correct
29 Correct 280 ms 81384 KB Output is correct
30 Correct 859 ms 171592 KB Output is correct
31 Correct 210 ms 41228 KB Output is correct
32 Correct 1 ms 712 KB Output is correct
33 Correct 1 ms 724 KB Output is correct
34 Correct 599 ms 197956 KB Output is correct
35 Correct 623 ms 197920 KB Output is correct
36 Correct 4 ms 1108 KB Output is correct
37 Correct 230 ms 40140 KB Output is correct
38 Correct 550 ms 163888 KB Output is correct
39 Correct 429 ms 94504 KB Output is correct
40 Correct 240 ms 66424 KB Output is correct
41 Correct 245 ms 66368 KB Output is correct
42 Correct 149 ms 66336 KB Output is correct
43 Runtime error 5 ms 1556 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 456 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 456 KB Output is correct
9 Correct 1 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 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 712 KB Output is correct
18 Correct 3 ms 1228 KB Output is correct
19 Correct 9 ms 2636 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 452 KB Output is correct
22 Correct 8 ms 4504 KB Output is correct
23 Correct 9 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 7 ms 3784 KB Output is correct
27 Correct 4 ms 1748 KB Output is correct
28 Correct 3 ms 1864 KB Output is correct
29 Correct 280 ms 81384 KB Output is correct
30 Correct 859 ms 171592 KB Output is correct
31 Correct 210 ms 41228 KB Output is correct
32 Correct 1 ms 712 KB Output is correct
33 Correct 1 ms 724 KB Output is correct
34 Correct 599 ms 197956 KB Output is correct
35 Correct 623 ms 197920 KB Output is correct
36 Correct 4 ms 1108 KB Output is correct
37 Correct 230 ms 40140 KB Output is correct
38 Correct 550 ms 163888 KB Output is correct
39 Correct 429 ms 94504 KB Output is correct
40 Correct 240 ms 66424 KB Output is correct
41 Correct 245 ms 66368 KB Output is correct
42 Correct 149 ms 66336 KB Output is correct
43 Runtime error 5 ms 1556 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -