Submission #935178

# Submission time Handle Problem Language Result Execution time Memory
935178 2024-02-28T18:55:21 Z kim Chorus (JOI23_chorus) C++17
100 / 100
1444 ms 55672 KB
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;
#define f first
#define s second
#define eb emplace_back

const ll inf=1e18;

int a[1000005],b[1000005],pos[1000005],pos2[1000005];
ll qs[1000005];
pair<ll,int> dp2[1000005];

struct line{
    ll m,c;
    int cnt;
    line(ll m,ll c,int cnt):m(m),c(c),cnt(cnt){}
    ll operator()(const ll x)const{return m*x+c;}
};
struct cht{
    deque<line> dq;
    bool check(line a,line b,line c){
        return (a.c-b.c)*(c.m-a.m)>=(a.c-c.c)*(b.m-a.m);
    }
    void insert(ll m,ll c,int cnt){
        line f(m,c,cnt);
        while(dq.size()>1&&check(dq.end()[-2],dq.back(),f)) dq.pop_back();
        dq.eb(f);
    }
    pair<ll,int> qr(ll x){
        if(dq.empty()) return {inf,0};
        while(dq.size()>1&&dq[1](x)<dq.front()(x)) dq.pop_front();
        return {dq.front()(x),dq.front().cnt};
    }
    void clear(){dq.clear();}
}dp;
void cal(int n,ll P){
    dp.clear();
    int cur=0;
    for(int i=1;i<=n;++i){
        while(cur<i&&pos2[cur+1]<=i){
            dp.insert(-cur,dp2[cur].f-qs[pos2[cur+1]-1]+1LL*pos2[cur+1]*(cur+1)-cur+P,dp2[cur].s+1);
            ++cur;
        }
        auto [val,cnt]=dp.qr(i);
        dp2[i]={val+qs[i]-i-1,cnt};
        if(cur<i) dp2[i]=min(dp2[i],{dp2[cur].f+P,dp2[cur].s+1});
    }
}

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

    int n,K; cin>>n>>K;
    string s; cin>>s;
    for(int i=0,ia=0,ib=0;i<s.length();++i){
        if(s[i]=='A') a[++ia]=i;
        else b[++ib]=i;
    }
    for(int i=1,j=1;i<=n;++i){
        while(j<=n&&b[j]<a[i]) ++j;
        pos[i]=j;
        qs[i]=qs[i-1]+j;
    }
    for(int i=1,j=1;i<=n;++i){
        while(j<=n&&pos[j]<=i) ++j;
        j=max(i,j);
        pos2[i]=j;
    }
    ll l=0,r=1LL*n*n;
    while(l<r){
        ll mid=l+r>>1;
        cal(n,mid);
        if(dp2[n].s>K) l=mid+1;
        else r=mid;
    }
    cal(n,l);
    cout<<dp2[n].f-K*l<<endl;

    return 0;
}

Compilation message

chorus.cpp: In function 'int main()':
chorus.cpp:57:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for(int i=0,ia=0,ib=0;i<s.length();++i){
      |                           ~^~~~~~~~~~~
chorus.cpp:73:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   73 |         ll mid=l+r>>1;
      |                ~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8540 KB Output is correct
29 Correct 4 ms 8796 KB Output is correct
30 Correct 6 ms 8872 KB Output is correct
31 Correct 6 ms 8796 KB Output is correct
32 Correct 3 ms 8884 KB Output is correct
33 Correct 3 ms 8664 KB Output is correct
34 Correct 4 ms 8948 KB Output is correct
35 Correct 3 ms 8796 KB Output is correct
36 Correct 6 ms 8872 KB Output is correct
37 Correct 7 ms 8796 KB Output is correct
38 Correct 4 ms 8884 KB Output is correct
39 Correct 5 ms 8796 KB Output is correct
40 Correct 4 ms 8796 KB Output is correct
41 Correct 4 ms 8844 KB Output is correct
42 Correct 3 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8540 KB Output is correct
29 Correct 4 ms 8796 KB Output is correct
30 Correct 6 ms 8872 KB Output is correct
31 Correct 6 ms 8796 KB Output is correct
32 Correct 3 ms 8884 KB Output is correct
33 Correct 3 ms 8664 KB Output is correct
34 Correct 4 ms 8948 KB Output is correct
35 Correct 3 ms 8796 KB Output is correct
36 Correct 6 ms 8872 KB Output is correct
37 Correct 7 ms 8796 KB Output is correct
38 Correct 4 ms 8884 KB Output is correct
39 Correct 5 ms 8796 KB Output is correct
40 Correct 4 ms 8796 KB Output is correct
41 Correct 4 ms 8844 KB Output is correct
42 Correct 3 ms 8796 KB Output is correct
43 Correct 64 ms 12124 KB Output is correct
44 Correct 110 ms 13028 KB Output is correct
45 Correct 117 ms 13008 KB Output is correct
46 Correct 48 ms 13276 KB Output is correct
47 Correct 56 ms 13152 KB Output is correct
48 Correct 59 ms 13152 KB Output is correct
49 Correct 56 ms 13152 KB Output is correct
50 Correct 130 ms 12992 KB Output is correct
51 Correct 119 ms 12776 KB Output is correct
52 Correct 65 ms 13148 KB Output is correct
53 Correct 67 ms 13152 KB Output is correct
54 Correct 117 ms 13032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 1 ms 8540 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8796 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 1 ms 8540 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 1 ms 8540 KB Output is correct
27 Correct 1 ms 8540 KB Output is correct
28 Correct 1 ms 8540 KB Output is correct
29 Correct 4 ms 8796 KB Output is correct
30 Correct 6 ms 8872 KB Output is correct
31 Correct 6 ms 8796 KB Output is correct
32 Correct 3 ms 8884 KB Output is correct
33 Correct 3 ms 8664 KB Output is correct
34 Correct 4 ms 8948 KB Output is correct
35 Correct 3 ms 8796 KB Output is correct
36 Correct 6 ms 8872 KB Output is correct
37 Correct 7 ms 8796 KB Output is correct
38 Correct 4 ms 8884 KB Output is correct
39 Correct 5 ms 8796 KB Output is correct
40 Correct 4 ms 8796 KB Output is correct
41 Correct 4 ms 8844 KB Output is correct
42 Correct 3 ms 8796 KB Output is correct
43 Correct 64 ms 12124 KB Output is correct
44 Correct 110 ms 13028 KB Output is correct
45 Correct 117 ms 13008 KB Output is correct
46 Correct 48 ms 13276 KB Output is correct
47 Correct 56 ms 13152 KB Output is correct
48 Correct 59 ms 13152 KB Output is correct
49 Correct 56 ms 13152 KB Output is correct
50 Correct 130 ms 12992 KB Output is correct
51 Correct 119 ms 12776 KB Output is correct
52 Correct 65 ms 13148 KB Output is correct
53 Correct 67 ms 13152 KB Output is correct
54 Correct 117 ms 13032 KB Output is correct
55 Correct 819 ms 41504 KB Output is correct
56 Correct 977 ms 54220 KB Output is correct
57 Correct 1365 ms 52656 KB Output is correct
58 Correct 564 ms 55672 KB Output is correct
59 Correct 609 ms 55540 KB Output is correct
60 Correct 664 ms 55536 KB Output is correct
61 Correct 660 ms 55536 KB Output is correct
62 Correct 1365 ms 52724 KB Output is correct
63 Correct 1412 ms 52720 KB Output is correct
64 Correct 756 ms 55524 KB Output is correct
65 Correct 725 ms 55500 KB Output is correct
66 Correct 1444 ms 52904 KB Output is correct
67 Correct 786 ms 55480 KB Output is correct