Submission #750519

# Submission time Handle Problem Language Result Execution time Memory
750519 2023-05-29T15:24:37 Z bachhoangxuan Chorus (JOI23_chorus) C++17
100 / 100
2726 ms 88508 KB
// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
const int inf=9e18;
const int mod=998244353;
const int mod2=1e9+7;
const int maxn=1e6+5;
const int bl=650;
const int maxs=650;
const int maxm=200005;
const int maxq=500005;
const int maxl=20;
const int maxa=1000005;
int power(int a,int n){
    int res=1;
    while(n){
        if(n&1) res=res*a%mod;
        a=a*a%mod;n>>=1;
    }
    return res;
}
struct line{
    int a,b,c,p;
    bool operator<(line l){return a>l.a;}
    bool operator<(int k){return p<k;}
};
struct cvht{
    line x[maxn];
    int sz=0,pos=1;
    int div(int a,int b){
        return a/b-((a^b)<0 && a%b);
    }
    bool isect(line &l,line y){
        if(l.a==y.a){
            if(l.b<y.b) l.p=inf;
            else if(l.b>y.b) l.p=-inf;
            else l.p=(l.c<y.c?inf:-inf);
        }
        else l.p=div(y.b-l.b,l.a-y.a);
        return l.p>=y.p;
    }
    void add(line l){
        if(sz>0) isect(x[sz],l);
        while(sz>=2 && x[sz-1].p>=x[sz].p){
            sz--;
            isect(x[sz],l);
        }
        x[++sz]=l;
    }
    line query(int v){
        auto l=*lower_bound(x+1,x+sz+1,v);
        //pos=min(pos,sz);
        //while(pos<=sz && x[pos].p<v) pos++;
        //while(pos>1 && x[pos-1].p>=v) pos--;
        return l;
    }
};
cvht cht;
int a[maxn],b[maxn],bb[maxn],f[maxn],sum[maxn],na,nb,n,k;
pii dp[maxn];
pii cal(int mid){
    cht.sz=0;cht.pos=1;
    int cur=0;
    deque<int> dq;
    dq.push_back(0);
    for(int i=1;i<=n;i++){
        dp[i]={inf,-1};
        while(cur<n && i>=f[cur+1]){
            cur++;
            cht.add({2*f[cur]-2*bb[cur],2*dp[cur-1].fi-2*sum[f[cur]-1]+2*bb[cur]*(f[cur]-1)-f[cur]*f[cur]+f[cur],dp[cur-1].se,inf});
            if(!dq.empty() && dq.front()==cur-1) dq.pop_front();
        }
        line l=cht.query(i);
        if(!dq.empty()) dp[i]={dp[dq.front()].fi+mid,dp[dq.front()].se+1};
        if(cht.sz!=0) dp[i]=min(dp[i],{(i*l.a+l.b+2*sum[i]-i*i-i)/2+mid,l.c+1});
        while(!dq.empty() && dp[dq.back()]>dp[i]) dq.pop_back();
        dq.push_back(i);
        //cout << dp[i].fi << ' ' << dp[i].se << ' ' << cur << '\n';
    }
    return dp[n];
}
void solve(){
    cin >> n >> k;
    for(int i=1;i<=2*n;i++){
        char c;cin >> c;
        if(c=='A') a[++na]=i;
        else b[++nb]=i;
    }
    for(int i=1;i<=n;i++){
        bb[i]=0;
        sum[i]=a[i]+sum[i-1];
        int l=i,r=n;
        while(r>=l){
            int mid=(r+l)>>1;
            if(a[i]>b[mid]){bb[i]=mid-i+1;l=mid+1;}
            else r=mid-1;
        }
        if(bb[i]==0) bb[i]=b[i];
        else bb[i]=a[i]-bb[i];
        l=i,r=n;
        f[i]=n+1;
        while(r>=l){
            int mid=(r+l)>>1;
            if(a[mid]>bb[i]){f[i]=mid;r=mid-1;}
            else l=mid+1;
        }
        //cout << f[i] << ' ' << bb[i] << '\n';
    }
    int l=0,r=2e12,Min=0,use=0;
    while(r>=l){
        int mid=(r+l)>>1;
        pii res=cal(mid);
        if(res.se<=k){Min=res.fi;use=mid;r=mid-1;}
        else l=mid+1;
    }
    cout << Min-k*use << '\n';
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int test=1;//cin >> test;
    while(test--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 8 ms 724 KB Output is correct
30 Correct 15 ms 748 KB Output is correct
31 Correct 12 ms 740 KB Output is correct
32 Correct 5 ms 740 KB Output is correct
33 Correct 10 ms 740 KB Output is correct
34 Correct 7 ms 724 KB Output is correct
35 Correct 7 ms 724 KB Output is correct
36 Correct 10 ms 596 KB Output is correct
37 Correct 11 ms 740 KB Output is correct
38 Correct 7 ms 724 KB Output is correct
39 Correct 9 ms 724 KB Output is correct
40 Correct 11 ms 708 KB Output is correct
41 Correct 7 ms 596 KB Output is correct
42 Correct 5 ms 728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 8 ms 724 KB Output is correct
30 Correct 15 ms 748 KB Output is correct
31 Correct 12 ms 740 KB Output is correct
32 Correct 5 ms 740 KB Output is correct
33 Correct 10 ms 740 KB Output is correct
34 Correct 7 ms 724 KB Output is correct
35 Correct 7 ms 724 KB Output is correct
36 Correct 10 ms 596 KB Output is correct
37 Correct 11 ms 740 KB Output is correct
38 Correct 7 ms 724 KB Output is correct
39 Correct 9 ms 724 KB Output is correct
40 Correct 11 ms 708 KB Output is correct
41 Correct 7 ms 596 KB Output is correct
42 Correct 5 ms 728 KB Output is correct
43 Correct 132 ms 4772 KB Output is correct
44 Correct 232 ms 7652 KB Output is correct
45 Correct 232 ms 7504 KB Output is correct
46 Correct 110 ms 7588 KB Output is correct
47 Correct 153 ms 7788 KB Output is correct
48 Correct 147 ms 9140 KB Output is correct
49 Correct 150 ms 9148 KB Output is correct
50 Correct 242 ms 7132 KB Output is correct
51 Correct 244 ms 7176 KB Output is correct
52 Correct 172 ms 9144 KB Output is correct
53 Correct 151 ms 9060 KB Output is correct
54 Correct 232 ms 7356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 8 ms 724 KB Output is correct
30 Correct 15 ms 748 KB Output is correct
31 Correct 12 ms 740 KB Output is correct
32 Correct 5 ms 740 KB Output is correct
33 Correct 10 ms 740 KB Output is correct
34 Correct 7 ms 724 KB Output is correct
35 Correct 7 ms 724 KB Output is correct
36 Correct 10 ms 596 KB Output is correct
37 Correct 11 ms 740 KB Output is correct
38 Correct 7 ms 724 KB Output is correct
39 Correct 9 ms 724 KB Output is correct
40 Correct 11 ms 708 KB Output is correct
41 Correct 7 ms 596 KB Output is correct
42 Correct 5 ms 728 KB Output is correct
43 Correct 132 ms 4772 KB Output is correct
44 Correct 232 ms 7652 KB Output is correct
45 Correct 232 ms 7504 KB Output is correct
46 Correct 110 ms 7588 KB Output is correct
47 Correct 153 ms 7788 KB Output is correct
48 Correct 147 ms 9140 KB Output is correct
49 Correct 150 ms 9148 KB Output is correct
50 Correct 242 ms 7132 KB Output is correct
51 Correct 244 ms 7176 KB Output is correct
52 Correct 172 ms 9144 KB Output is correct
53 Correct 151 ms 9060 KB Output is correct
54 Correct 232 ms 7356 KB Output is correct
55 Correct 1877 ms 46828 KB Output is correct
56 Correct 2471 ms 81316 KB Output is correct
57 Correct 2554 ms 72876 KB Output is correct
58 Correct 1207 ms 72736 KB Output is correct
59 Correct 1622 ms 74116 KB Output is correct
60 Correct 1696 ms 88396 KB Output is correct
61 Correct 1642 ms 88352 KB Output is correct
62 Correct 2726 ms 68780 KB Output is correct
63 Correct 2666 ms 69440 KB Output is correct
64 Correct 1798 ms 88508 KB Output is correct
65 Correct 1769 ms 88380 KB Output is correct
66 Correct 2668 ms 71368 KB Output is correct
67 Correct 1818 ms 88404 KB Output is correct