답안 #925115

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
925115 2024-02-10T19:18:56 Z ttamx Chorus (JOI23_chorus) C++14
100 / 100
1584 ms 45808 KB
#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using pl = pair<ll,ll>;

const int N=1e6+5;

int n,k;
string s;
int a[N],b[N];
ll qs[N];
pl dp[N];

struct Line{
    ll m,c,cnt;
    Line(ll _m,ll _c,ll _cnt):m(_m),c(_c),cnt(_cnt){}
    ll eval(ll x){
        return m*x+c;
    }
};

struct CHT{
    deque<Line> hull;
    bool bad(Line x,Line y,Line z){
        return (x.c-y.c)*(z.m-y.m)>(y.c-z.c)*(y.m-x.m);
    }
    void insert(ll m,ll c,ll cnt){
        Line v(m,c,cnt);
        while(hull.size()>1&&bad(hull.end()[-2],hull.back(),v))hull.pop_back();
        hull.emplace_back(v);
    }
    pl query(ll x){
        while(hull.size()>1&&hull[0].eval(x)>hull[1].eval(x))hull.pop_front();
        return pl(hull[0].eval(x),hull[0].cnt);
    }
};

pl solve(ll lambda){
    CHT cht;
    cht.insert(0,0,0);
    deque<int> dq;
    for(int i=1;i<=n;i++){
        while(!dq.empty()&&b[dq.front()]<i){
            int j=dq.front();
            dq.pop_front();
            cht.insert(-j,dp[j].first-qs[b[j]]+1LL*j*b[j],dp[j].second);
        }
        dp[i]=cht.query(i);
        dp[i].first+=qs[i]+lambda;
        dp[i].second++;
        dq.emplace_back(i);
    }
    return dp[n];
}

int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n >> k >> s;
    for(int i=0,ia=0,ib=0;i<2*n;i++){
        if(s[i]=='A'){
            a[++ia]=ib;
        }else{
            ib++;
        }
    }
    for(int i=1,p=1;i<=n;i++){
        while(p<=n&&a[p]<i)p++;
        b[i]=max(i,p-1);
    }
    for(int i=1;i<=n;i++)qs[i]=qs[i-1]+a[i];
    ll l=0,r=1LL*n*n;
    while(l<r){
        ll m=(l+r)/2;
        if(solve(m).second<=k)r=m;
        else l=m+1;
    }
    cout << solve(l).first-k*l;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 3 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 3 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 3 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 3 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4440 KB Output is correct
29 Correct 4 ms 4700 KB Output is correct
30 Correct 5 ms 4768 KB Output is correct
31 Correct 5 ms 4852 KB Output is correct
32 Correct 3 ms 4584 KB Output is correct
33 Correct 3 ms 4700 KB Output is correct
34 Correct 3 ms 4700 KB Output is correct
35 Correct 4 ms 4696 KB Output is correct
36 Correct 5 ms 4700 KB Output is correct
37 Correct 6 ms 4772 KB Output is correct
38 Correct 3 ms 4696 KB Output is correct
39 Correct 6 ms 4652 KB Output is correct
40 Correct 3 ms 4700 KB Output is correct
41 Correct 3 ms 4700 KB Output is correct
42 Correct 2 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 3 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 3 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4440 KB Output is correct
29 Correct 4 ms 4700 KB Output is correct
30 Correct 5 ms 4768 KB Output is correct
31 Correct 5 ms 4852 KB Output is correct
32 Correct 3 ms 4584 KB Output is correct
33 Correct 3 ms 4700 KB Output is correct
34 Correct 3 ms 4700 KB Output is correct
35 Correct 4 ms 4696 KB Output is correct
36 Correct 5 ms 4700 KB Output is correct
37 Correct 6 ms 4772 KB Output is correct
38 Correct 3 ms 4696 KB Output is correct
39 Correct 6 ms 4652 KB Output is correct
40 Correct 3 ms 4700 KB Output is correct
41 Correct 3 ms 4700 KB Output is correct
42 Correct 2 ms 4700 KB Output is correct
43 Correct 71 ms 8100 KB Output is correct
44 Correct 127 ms 8876 KB Output is correct
45 Correct 125 ms 8908 KB Output is correct
46 Correct 61 ms 8956 KB Output is correct
47 Correct 63 ms 8872 KB Output is correct
48 Correct 67 ms 8872 KB Output is correct
49 Correct 71 ms 8872 KB Output is correct
50 Correct 128 ms 8872 KB Output is correct
51 Correct 133 ms 8868 KB Output is correct
52 Correct 72 ms 8872 KB Output is correct
53 Correct 70 ms 8872 KB Output is correct
54 Correct 130 ms 8872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 3 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 3 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4440 KB Output is correct
29 Correct 4 ms 4700 KB Output is correct
30 Correct 5 ms 4768 KB Output is correct
31 Correct 5 ms 4852 KB Output is correct
32 Correct 3 ms 4584 KB Output is correct
33 Correct 3 ms 4700 KB Output is correct
34 Correct 3 ms 4700 KB Output is correct
35 Correct 4 ms 4696 KB Output is correct
36 Correct 5 ms 4700 KB Output is correct
37 Correct 6 ms 4772 KB Output is correct
38 Correct 3 ms 4696 KB Output is correct
39 Correct 6 ms 4652 KB Output is correct
40 Correct 3 ms 4700 KB Output is correct
41 Correct 3 ms 4700 KB Output is correct
42 Correct 2 ms 4700 KB Output is correct
43 Correct 71 ms 8100 KB Output is correct
44 Correct 127 ms 8876 KB Output is correct
45 Correct 125 ms 8908 KB Output is correct
46 Correct 61 ms 8956 KB Output is correct
47 Correct 63 ms 8872 KB Output is correct
48 Correct 67 ms 8872 KB Output is correct
49 Correct 71 ms 8872 KB Output is correct
50 Correct 128 ms 8872 KB Output is correct
51 Correct 133 ms 8868 KB Output is correct
52 Correct 72 ms 8872 KB Output is correct
53 Correct 70 ms 8872 KB Output is correct
54 Correct 130 ms 8872 KB Output is correct
55 Correct 918 ms 35280 KB Output is correct
56 Correct 1132 ms 45764 KB Output is correct
57 Correct 1500 ms 45624 KB Output is correct
58 Correct 700 ms 45808 KB Output is correct
59 Correct 776 ms 45620 KB Output is correct
60 Correct 806 ms 45748 KB Output is correct
61 Correct 800 ms 45664 KB Output is correct
62 Correct 1520 ms 45748 KB Output is correct
63 Correct 1584 ms 45752 KB Output is correct
64 Correct 811 ms 45760 KB Output is correct
65 Correct 801 ms 45728 KB Output is correct
66 Correct 1555 ms 45748 KB Output is correct
67 Correct 793 ms 45652 KB Output is correct