Submission #953475

# Submission time Handle Problem Language Result Execution time Memory
953475 2024-03-26T03:02:19 Z owoovo Chorus (JOI23_chorus) C++17
100 / 100
4663 ms 47500 KB
#include<bits/stdc++.h>
#define ll long long
#define F first 
#define S second 
using namespace std;
const ll maxn=1e18;
ll n,k,bto[1000010],bhh[1000010],hhsum[1000010];
pair<ll,ll> dp[1000010];
ll cost(ll i,ll j){
    if(i>=j)return maxn;
    ll l=max(i,bto[i]),r=j;
    return hhsum[r]-hhsum[l]-i*(r-l);
}
struct seg{
    ll pos,l,r;
    seg(ll _pos,ll _l,ll _r){
        pos=_pos,l=_l,r=_r;
    }
};
bool check(ll d){
    for(int i=1;i<=n;i++)dp[i]={maxn,0};
    deque<seg> sg;
    sg.push_back({0,1,n});
    for(int i=1;i<=n;i++){
        while(sg.front().r<i)sg.pop_front();
        ll pos=sg.front().pos;
        dp[i]={dp[pos].F+cost(pos,i)+d,dp[pos].S+1};
        while(!sg.empty()&&dp[i].F+cost(i,sg.back().l)<dp[sg.back().pos].F+cost(sg.back().pos,sg.back().l)){
            sg.pop_back();
        }
        if(sg.empty()){
            sg.push_back({i,i+1,n});
        }else{
            auto[pos,l,r]=sg.back();
            sg.pop_back();
            ll L=l,R=r;
            while(L!=R){
                int mid=(L+R+1)>>1;
                if(dp[i].F+cost(i,mid)<dp[pos].F+cost(pos,mid)){
                    R=mid-1;
                }else{
                    L=mid;
                }
            }
            sg.push_back({pos,l,L});
            if(L!=n)sg.push_back({i,L+1,n});
        }
    }
    // for(int i=1;i<=n;i++){
    //     cout<<dp[i].F-d*(dp[i].S)<<" ";
    // }
    // cout<<"\n";
    return dp[n].S>k;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int nowa=0,nowb=0;
    cin>>n>>k;
    for(int i=0;i<2*n;i++){
        char c;
        cin>>c;
        if(c=='A'){
            nowa++;
            bhh[nowa]=nowb;
        }else{
            nowb++;
            bto[nowb]=nowa;
        }
    }
    for(int i=1;i<=n;i++){
        hhsum[i]=hhsum[i-1]+bhh[i];
    }
    ll l=0,r=maxn;
    while(l!=r){
        ll m=(l+r)>>1;
        if(check(m)){
            l=m+1;
        }else{
            r=m;
        }
    }
    check(l);
    cout<<dp[n].F-l*k<<"\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4448 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4452 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4560 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 4448 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4560 KB Output is correct
13 Correct 1 ms 4448 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 2 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4448 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4452 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4560 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 4448 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4560 KB Output is correct
13 Correct 1 ms 4448 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 2 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 2 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 2 ms 4440 KB Output is correct
22 Correct 3 ms 4444 KB Output is correct
23 Correct 2 ms 4444 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 3 ms 4444 KB Output is correct
27 Correct 2 ms 4448 KB Output is correct
28 Correct 2 ms 4440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4448 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4452 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4560 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 4448 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4560 KB Output is correct
13 Correct 1 ms 4448 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 2 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 2 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 2 ms 4440 KB Output is correct
22 Correct 3 ms 4444 KB Output is correct
23 Correct 2 ms 4444 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 3 ms 4444 KB Output is correct
27 Correct 2 ms 4448 KB Output is correct
28 Correct 2 ms 4440 KB Output is correct
29 Correct 18 ms 4700 KB Output is correct
30 Correct 17 ms 4700 KB Output is correct
31 Correct 17 ms 4696 KB Output is correct
32 Correct 15 ms 4704 KB Output is correct
33 Correct 14 ms 4700 KB Output is correct
34 Correct 16 ms 4704 KB Output is correct
35 Correct 16 ms 4736 KB Output is correct
36 Correct 24 ms 4700 KB Output is correct
37 Correct 16 ms 4700 KB Output is correct
38 Correct 16 ms 4736 KB Output is correct
39 Correct 16 ms 4696 KB Output is correct
40 Correct 17 ms 4696 KB Output is correct
41 Correct 15 ms 4700 KB Output is correct
42 Correct 16 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4448 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4452 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4560 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 4448 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4560 KB Output is correct
13 Correct 1 ms 4448 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 2 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 2 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 2 ms 4440 KB Output is correct
22 Correct 3 ms 4444 KB Output is correct
23 Correct 2 ms 4444 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 3 ms 4444 KB Output is correct
27 Correct 2 ms 4448 KB Output is correct
28 Correct 2 ms 4440 KB Output is correct
29 Correct 18 ms 4700 KB Output is correct
30 Correct 17 ms 4700 KB Output is correct
31 Correct 17 ms 4696 KB Output is correct
32 Correct 15 ms 4704 KB Output is correct
33 Correct 14 ms 4700 KB Output is correct
34 Correct 16 ms 4704 KB Output is correct
35 Correct 16 ms 4736 KB Output is correct
36 Correct 24 ms 4700 KB Output is correct
37 Correct 16 ms 4700 KB Output is correct
38 Correct 16 ms 4736 KB Output is correct
39 Correct 16 ms 4696 KB Output is correct
40 Correct 17 ms 4696 KB Output is correct
41 Correct 15 ms 4700 KB Output is correct
42 Correct 16 ms 4700 KB Output is correct
43 Correct 213 ms 7504 KB Output is correct
44 Correct 368 ms 8052 KB Output is correct
45 Correct 369 ms 8100 KB Output is correct
46 Correct 323 ms 7896 KB Output is correct
47 Correct 325 ms 8320 KB Output is correct
48 Correct 368 ms 8164 KB Output is correct
49 Correct 376 ms 8420 KB Output is correct
50 Correct 424 ms 8068 KB Output is correct
51 Correct 383 ms 8164 KB Output is correct
52 Correct 366 ms 8168 KB Output is correct
53 Correct 375 ms 8180 KB Output is correct
54 Correct 368 ms 8036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4448 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4452 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4560 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 4448 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4560 KB Output is correct
13 Correct 1 ms 4448 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 2 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 2 ms 4444 KB Output is correct
20 Correct 2 ms 4444 KB Output is correct
21 Correct 2 ms 4440 KB Output is correct
22 Correct 3 ms 4444 KB Output is correct
23 Correct 2 ms 4444 KB Output is correct
24 Correct 2 ms 4444 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 3 ms 4444 KB Output is correct
27 Correct 2 ms 4448 KB Output is correct
28 Correct 2 ms 4440 KB Output is correct
29 Correct 18 ms 4700 KB Output is correct
30 Correct 17 ms 4700 KB Output is correct
31 Correct 17 ms 4696 KB Output is correct
32 Correct 15 ms 4704 KB Output is correct
33 Correct 14 ms 4700 KB Output is correct
34 Correct 16 ms 4704 KB Output is correct
35 Correct 16 ms 4736 KB Output is correct
36 Correct 24 ms 4700 KB Output is correct
37 Correct 16 ms 4700 KB Output is correct
38 Correct 16 ms 4736 KB Output is correct
39 Correct 16 ms 4696 KB Output is correct
40 Correct 17 ms 4696 KB Output is correct
41 Correct 15 ms 4700 KB Output is correct
42 Correct 16 ms 4700 KB Output is correct
43 Correct 213 ms 7504 KB Output is correct
44 Correct 368 ms 8052 KB Output is correct
45 Correct 369 ms 8100 KB Output is correct
46 Correct 323 ms 7896 KB Output is correct
47 Correct 325 ms 8320 KB Output is correct
48 Correct 368 ms 8164 KB Output is correct
49 Correct 376 ms 8420 KB Output is correct
50 Correct 424 ms 8068 KB Output is correct
51 Correct 383 ms 8164 KB Output is correct
52 Correct 366 ms 8168 KB Output is correct
53 Correct 375 ms 8180 KB Output is correct
54 Correct 368 ms 8036 KB Output is correct
55 Correct 2661 ms 34416 KB Output is correct
56 Correct 4365 ms 46824 KB Output is correct
57 Correct 4038 ms 45992 KB Output is correct
58 Correct 3782 ms 41784 KB Output is correct
59 Correct 3353 ms 47500 KB Output is correct
60 Correct 4529 ms 47260 KB Output is correct
61 Correct 4623 ms 47276 KB Output is correct
62 Correct 4048 ms 45956 KB Output is correct
63 Correct 4148 ms 46072 KB Output is correct
64 Correct 4518 ms 47228 KB Output is correct
65 Correct 4663 ms 47232 KB Output is correct
66 Correct 4224 ms 46036 KB Output is correct
67 Correct 4519 ms 47472 KB Output is correct