Submission #902769

# Submission time Handle Problem Language Result Execution time Memory
902769 2024-01-11T02:26:23 Z Tuanlinh123 Chorus (JOI23_chorus) C++17
100 / 100
3047 ms 86620 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,popcnt,lzcnt")
#include<bits/stdc++.h>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
 
const long long inf=LLONG_MAX, maxn=1e6+5;
long long cnt[maxn], to[maxn];
long long n, k, dp[maxn], pre[maxn];
 
struct Line
{
    mutable long long k, m, p, idx;
    bool operator < (const Line &o) const{return k<o.k;}
    bool operator < (long long o) const {return p<o;} 
};
 
struct CHT
{
    int csz = 0;
    vector <Line> data;
    long long floordiv(long long a, long long b){return a/b-(((a^b)<0) && (a%b));}
    long long isect(Line x, Line y) {
        if (x.k == y.k) return (x.m > y.m ? inf : -inf);
        return floordiv(x.m - y.m, y.k - x.k);
    }
    void add(long long k, long long m, ll idx)
    {
        k = -k, m = -m;
        Line L = {k, m, inf, idx};
        while (csz >= 2 and isect(data[csz - 2], data[csz - 1]) >= isect(data[csz - 1], L)) {
            --csz; data.pop_back();
            if (csz >= 2) data[csz - 2].p = isect(data[csz - 2], data[csz - 1]);
        }
        if (csz) data.back().p = isect(data.back(), L);
        ++csz; data.push_back(L);
    }
    pair<long long, ll> query(ll x)
    {
        auto L = *lower_bound(data.begin(), data.end(), x);
        return {-L.k*x-L.m, L.idx};
    }
};

pair<long long, ll> solve(long long C)
{
    ll crr=1;
    CHT A; A.add(0, -pre[to[0]-1], 0);
    for (ll i=1; i<=n; i++)
        dp[i]=inf, cnt[i]=0;
    for (ll i=1; i<=n; i++)
    {
        while (crr<i && to[crr]<=i)
            A.add(-crr, -(pre[to[crr]-1]-(to[crr]-1)*crr)+dp[crr], cnt[crr]), crr++;
        tie(dp[i], cnt[i])=A.query(i); 
        dp[i]+=C+pre[i], cnt[i]++;
        if (crr<i && dp[crr]+C<dp[i])
            dp[i]=dp[crr]+C, cnt[i]=cnt[crr]+1;
    }
    return {dp[n], cnt[n]};
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n >> k;
    string s; cin >> s;
    long long x=0, y=0, crr=1;
    for (ll i=0; i<n*2; i++)
    {
        if (s[i]=='A')
            x++, pre[x]=pre[x-1]+y;
        else y++;
    }
    for (ll i=1; i<=n; i++)
        while (crr<=pre[i]-pre[i-1])
            to[crr]=i, crr++;
    while (crr<=n) to[crr]=n+1, crr++;
    for (ll i=0; i<=n; i++)
        if (to[i]<=i)
            to[i]=i+1;
    long long lo=0, hi=1LL*n*n;
    while (hi>lo)
    {
        long long mid=(lo+hi)/2;
        if (solve(mid).se<=k)
            hi=mid;
        else lo=mid+1;
    }
    cout << 0LL+solve(lo).fi-lo*k;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6488 KB Output is correct
24 Correct 1 ms 6496 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6504 KB Output is correct
28 Correct 1 ms 6504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6488 KB Output is correct
24 Correct 1 ms 6496 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6504 KB Output is correct
28 Correct 1 ms 6504 KB Output is correct
29 Correct 6 ms 6932 KB Output is correct
30 Correct 8 ms 6884 KB Output is correct
31 Correct 9 ms 7128 KB Output is correct
32 Correct 4 ms 6892 KB Output is correct
33 Correct 6 ms 6896 KB Output is correct
34 Correct 6 ms 7152 KB Output is correct
35 Correct 6 ms 7092 KB Output is correct
36 Correct 8 ms 6748 KB Output is correct
37 Correct 8 ms 6868 KB Output is correct
38 Correct 6 ms 7096 KB Output is correct
39 Correct 7 ms 6912 KB Output is correct
40 Correct 6 ms 6808 KB Output is correct
41 Correct 6 ms 6760 KB Output is correct
42 Correct 3 ms 6760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6488 KB Output is correct
24 Correct 1 ms 6496 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6504 KB Output is correct
28 Correct 1 ms 6504 KB Output is correct
29 Correct 6 ms 6932 KB Output is correct
30 Correct 8 ms 6884 KB Output is correct
31 Correct 9 ms 7128 KB Output is correct
32 Correct 4 ms 6892 KB Output is correct
33 Correct 6 ms 6896 KB Output is correct
34 Correct 6 ms 7152 KB Output is correct
35 Correct 6 ms 7092 KB Output is correct
36 Correct 8 ms 6748 KB Output is correct
37 Correct 8 ms 6868 KB Output is correct
38 Correct 6 ms 7096 KB Output is correct
39 Correct 7 ms 6912 KB Output is correct
40 Correct 6 ms 6808 KB Output is correct
41 Correct 6 ms 6760 KB Output is correct
42 Correct 3 ms 6760 KB Output is correct
43 Correct 129 ms 10996 KB Output is correct
44 Correct 232 ms 14748 KB Output is correct
45 Correct 236 ms 14688 KB Output is correct
46 Correct 136 ms 14800 KB Output is correct
47 Correct 142 ms 15264 KB Output is correct
48 Correct 181 ms 19136 KB Output is correct
49 Correct 165 ms 19336 KB Output is correct
50 Correct 232 ms 14328 KB Output is correct
51 Correct 238 ms 14364 KB Output is correct
52 Correct 173 ms 18320 KB Output is correct
53 Correct 180 ms 18784 KB Output is correct
54 Correct 234 ms 14568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6488 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 2 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6488 KB Output is correct
24 Correct 1 ms 6496 KB Output is correct
25 Correct 2 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6504 KB Output is correct
28 Correct 1 ms 6504 KB Output is correct
29 Correct 6 ms 6932 KB Output is correct
30 Correct 8 ms 6884 KB Output is correct
31 Correct 9 ms 7128 KB Output is correct
32 Correct 4 ms 6892 KB Output is correct
33 Correct 6 ms 6896 KB Output is correct
34 Correct 6 ms 7152 KB Output is correct
35 Correct 6 ms 7092 KB Output is correct
36 Correct 8 ms 6748 KB Output is correct
37 Correct 8 ms 6868 KB Output is correct
38 Correct 6 ms 7096 KB Output is correct
39 Correct 7 ms 6912 KB Output is correct
40 Correct 6 ms 6808 KB Output is correct
41 Correct 6 ms 6760 KB Output is correct
42 Correct 3 ms 6760 KB Output is correct
43 Correct 129 ms 10996 KB Output is correct
44 Correct 232 ms 14748 KB Output is correct
45 Correct 236 ms 14688 KB Output is correct
46 Correct 136 ms 14800 KB Output is correct
47 Correct 142 ms 15264 KB Output is correct
48 Correct 181 ms 19136 KB Output is correct
49 Correct 165 ms 19336 KB Output is correct
50 Correct 232 ms 14328 KB Output is correct
51 Correct 238 ms 14364 KB Output is correct
52 Correct 173 ms 18320 KB Output is correct
53 Correct 180 ms 18784 KB Output is correct
54 Correct 234 ms 14568 KB Output is correct
55 Correct 1768 ms 58132 KB Output is correct
56 Correct 2564 ms 83420 KB Output is correct
57 Correct 2862 ms 68328 KB Output is correct
58 Correct 1504 ms 67840 KB Output is correct
59 Correct 2013 ms 85180 KB Output is correct
60 Correct 2122 ms 86484 KB Output is correct
61 Correct 2240 ms 85848 KB Output is correct
62 Correct 2907 ms 65024 KB Output is correct
63 Correct 3047 ms 65884 KB Output is correct
64 Correct 2057 ms 86192 KB Output is correct
65 Correct 2041 ms 85164 KB Output is correct
66 Correct 2872 ms 67424 KB Output is correct
67 Correct 2095 ms 86620 KB Output is correct