Submission #1037836

# Submission time Handle Problem Language Result Execution time Memory
1037836 2024-07-29T09:01:12 Z 정민찬(#10982) Chorus (JOI23_chorus) C++17
61 / 100
1341 ms 1048576 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;

struct Line{
    ll a, b;
    ll Get(ll x) { return a*x + b; }
    ld cross(Line other) {
        return (ld)(b - other.b) / (ld)(other.a - a);
    }
};

struct LineContainer{
    vector<Line> lines;
    ll idx;
    void Push(Line p) {
        while (lines.size() >= 2 && lines[lines.size()-2].cross(lines.back()) >= lines.back().cross(p)) {
            lines.pop_back();
            if (idx >= lines.size()) idx --;
        }
        lines.push_back(p);
    }
    ll Query(ll x) {
        while (idx+1 < lines.size() && lines[idx+1].Get(x) <= lines[idx].Get(x)) {
            idx ++;
        }
        return lines[idx].Get(x);
    }
};

ll chk[2000010];
ll S[2000010], cnt[2000010];
ll dp[10010][10010];

LineContainer cht[10010];

int main() {
    ios_base :: sync_with_stdio(false); cin.tie(NULL);
    ll N, k;
    cin >> N >> k;
    string s;
    cin >> s;
    vector<int> op;
    for (int i=0; i<2*N; i++) {
        if (s[i] == 'A') {
            op.push_back(i);
        }
    }
    reverse(op.begin(), op.end());
    ll pref = 0;
    ll ans = 0;
    vector<ll> a = {0};
    for (int i=0; i<2*N; i++) {
        if (chk[i]) continue;
        if (pref == 0 && s[i] == 'B') {
            ans += op.back() - (ll)a.size() + 1;
            a.push_back(1);
            chk[op.back()] = 1;
            op.pop_back();
            pref ++;
            i --;
            continue;
        }
        if (s[i] == 'A') {
            pref ++;
            a.push_back(1);
            op.pop_back();
        }
        else {
            pref --;
            a.push_back(0);
        }
    }
    vector<ll> li = {0}, ri = {0};
    for (ll i=1; i<=2*N; i++) {
        if (a[i]) li.push_back(i);
        else ri.push_back(i);
    }
    vector<ll> arr(N+1);
    arr[1] = 1;
    for (ll i=2; i<=N; i++) {
        arr[i] = arr[i-1];
        while (ri[arr[i]] < li[i]) arr[i] ++;
    }
    ll rp = 1;
    for (ll i=1; i<=N; i++) {
        while (rp+1 <= N && arr[rp+1] <= i) rp ++;
        cnt[i] = rp;
    }
    for (ll i=1; i<=N; i++) {
        S[i] += S[i-1] + arr[i];
    }
    cht[0].Push({-1, 0});
    for (ll i=1; i<=N; i++) {
        for (ll j=1; j<=min(i, k); j++) {
            dp[i][j] = cht[j-1].Query(i) + S[i];
            if (j != 1) dp[i][j] = min(dp[i][j], dp[i][j-1]);
        }
        for (ll j=1; j<=min(i, k); j++) {
            cht[j].Push({-i-1, dp[i][j] - S[i] + i*i + i + (cnt[i] - i) * (i+1) - (S[cnt[i]] - S[i])});
        }
    }

    cout << dp[N][k] + ans << '\n';
}

Compilation message

chorus.cpp: In member function 'void LineContainer::Push(Line)':
chorus.cpp:22:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |             if (idx >= lines.size()) idx --;
      |                 ~~~~^~~~~~~~~~~~~~~
chorus.cpp: In member function 'll LineContainer::Query(ll)':
chorus.cpp:27:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |         while (idx+1 < lines.size() && lines[idx+1].Get(x) <= lines[idx].Get(x)) {
      |                ~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4696 KB Output is correct
15 Correct 1 ms 4696 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4696 KB Output is correct
15 Correct 1 ms 4696 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 2 ms 16988 KB Output is correct
18 Correct 5 ms 44124 KB Output is correct
19 Correct 6 ms 44892 KB Output is correct
20 Correct 4 ms 43612 KB Output is correct
21 Correct 4 ms 43612 KB Output is correct
22 Correct 8 ms 46172 KB Output is correct
23 Correct 8 ms 46408 KB Output is correct
24 Correct 4 ms 43788 KB Output is correct
25 Correct 8 ms 46168 KB Output is correct
26 Correct 8 ms 46168 KB Output is correct
27 Correct 5 ms 44380 KB Output is correct
28 Correct 6 ms 44380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4696 KB Output is correct
15 Correct 1 ms 4696 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 2 ms 16988 KB Output is correct
18 Correct 5 ms 44124 KB Output is correct
19 Correct 6 ms 44892 KB Output is correct
20 Correct 4 ms 43612 KB Output is correct
21 Correct 4 ms 43612 KB Output is correct
22 Correct 8 ms 46172 KB Output is correct
23 Correct 8 ms 46408 KB Output is correct
24 Correct 4 ms 43788 KB Output is correct
25 Correct 8 ms 46168 KB Output is correct
26 Correct 8 ms 46168 KB Output is correct
27 Correct 5 ms 44380 KB Output is correct
28 Correct 6 ms 44380 KB Output is correct
29 Correct 291 ms 524716 KB Output is correct
30 Correct 403 ms 523856 KB Output is correct
31 Correct 150 ms 446544 KB Output is correct
32 Correct 30 ms 395100 KB Output is correct
33 Correct 32 ms 395324 KB Output is correct
34 Correct 381 ms 631332 KB Output is correct
35 Correct 376 ms 631380 KB Output is correct
36 Correct 37 ms 395348 KB Output is correct
37 Correct 156 ms 452100 KB Output is correct
38 Correct 371 ms 626768 KB Output is correct
39 Correct 313 ms 522320 KB Output is correct
40 Correct 198 ms 445524 KB Output is correct
41 Correct 197 ms 445608 KB Output is correct
42 Correct 181 ms 421180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4696 KB Output is correct
15 Correct 1 ms 4696 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 2 ms 16988 KB Output is correct
18 Correct 5 ms 44124 KB Output is correct
19 Correct 6 ms 44892 KB Output is correct
20 Correct 4 ms 43612 KB Output is correct
21 Correct 4 ms 43612 KB Output is correct
22 Correct 8 ms 46172 KB Output is correct
23 Correct 8 ms 46408 KB Output is correct
24 Correct 4 ms 43788 KB Output is correct
25 Correct 8 ms 46168 KB Output is correct
26 Correct 8 ms 46168 KB Output is correct
27 Correct 5 ms 44380 KB Output is correct
28 Correct 6 ms 44380 KB Output is correct
29 Correct 291 ms 524716 KB Output is correct
30 Correct 403 ms 523856 KB Output is correct
31 Correct 150 ms 446544 KB Output is correct
32 Correct 30 ms 395100 KB Output is correct
33 Correct 32 ms 395324 KB Output is correct
34 Correct 381 ms 631332 KB Output is correct
35 Correct 376 ms 631380 KB Output is correct
36 Correct 37 ms 395348 KB Output is correct
37 Correct 156 ms 452100 KB Output is correct
38 Correct 371 ms 626768 KB Output is correct
39 Correct 313 ms 522320 KB Output is correct
40 Correct 198 ms 445524 KB Output is correct
41 Correct 197 ms 445608 KB Output is correct
42 Correct 181 ms 421180 KB Output is correct
43 Runtime error 1341 ms 1048576 KB Execution killed with signal 9
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4696 KB Output is correct
15 Correct 1 ms 4696 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 2 ms 16988 KB Output is correct
18 Correct 5 ms 44124 KB Output is correct
19 Correct 6 ms 44892 KB Output is correct
20 Correct 4 ms 43612 KB Output is correct
21 Correct 4 ms 43612 KB Output is correct
22 Correct 8 ms 46172 KB Output is correct
23 Correct 8 ms 46408 KB Output is correct
24 Correct 4 ms 43788 KB Output is correct
25 Correct 8 ms 46168 KB Output is correct
26 Correct 8 ms 46168 KB Output is correct
27 Correct 5 ms 44380 KB Output is correct
28 Correct 6 ms 44380 KB Output is correct
29 Correct 291 ms 524716 KB Output is correct
30 Correct 403 ms 523856 KB Output is correct
31 Correct 150 ms 446544 KB Output is correct
32 Correct 30 ms 395100 KB Output is correct
33 Correct 32 ms 395324 KB Output is correct
34 Correct 381 ms 631332 KB Output is correct
35 Correct 376 ms 631380 KB Output is correct
36 Correct 37 ms 395348 KB Output is correct
37 Correct 156 ms 452100 KB Output is correct
38 Correct 371 ms 626768 KB Output is correct
39 Correct 313 ms 522320 KB Output is correct
40 Correct 198 ms 445524 KB Output is correct
41 Correct 197 ms 445608 KB Output is correct
42 Correct 181 ms 421180 KB Output is correct
43 Runtime error 1341 ms 1048576 KB Execution killed with signal 9
44 Halted 0 ms 0 KB -