Submission #901744

# Submission time Handle Problem Language Result Execution time Memory
901744 2024-01-10T01:48:02 Z Tuanlinh123 Chorus (JOI23_chorus) C++17
87 / 100
7000 ms 856352 KB
#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=2e9, maxn=1e6+5;
long long n, k, to[maxn];
long long pre[maxn];

struct Line
{
    long long a=inf, b=inf;
    ll idx=0;

    Line(){}
    Line(long long a, long long b, ll idx): a(a), b(b), idx(idx){}
    pair<long long, ll> ask(ll x)
    {
        if (a==inf) return {inf*inf, idx};
        return {a*x+b, idx};
    }
};

struct Node
{
    Line A;
    Node *l, *r;
};

struct Lichao
{
    Node *root=new Node();
    void addline(Line A)
    {
        ll Start=-inf, End=inf;
        Node *i=root;
        while (1)
        {
            if (A.a==inf) break;
            ll mid=(Start+End)/2;
            bool l=A.ask(Start).fi<i->A.ask(Start).fi;
            bool m=A.ask(mid).fi<i->A.ask(mid).fi;
            if (m) swap(A, i->A);
            if (Start+1!=End)
            {
                if (l!=m)
                {
                    if (!i->l)
                        i->l=new Node();
                    i=i->l;
                    End=mid;
                }
                else
                {
                    if (!i->r)
                        i->r=new Node();
                    i=i->r;
                    Start=mid;
                }
            }
            else break;
        }
    }

    pair<long long, ll> query(ll x)
    {
        Node *i=root;
        ll Start=-inf, End=inf;
        pair<long long, ll> ans={inf*inf, 0};
        while (1)
        {
            ll mid=(Start+End)/2;
            ans=min(ans, i->A.ask(x));
            if (x<mid && i->l)
            {
                i=i->l;
                End=mid;
            }
            else if (x>=mid && i->r)
            {
                i=i->r;
                Start=mid;
            }
            else break;
        }
        return ans;
    }
};

pair<long long, ll> solve(long long C)
{
    ll crr=1;
    Lichao A; A.addline(Line(0, -pre[to[0]-1], 0));
    vector <long long> dp(n+1, inf*inf); dp[0]=0;
    vector <ll> cnt(n+1, 0); 
    for (ll i=1; i<=n; i++)
    {
        while (crr<i && to[crr]<=i)
            A.addline(Line(-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;
        // cout << i << " " << dp[i] << " " << cnt[i] << "\n";
    }
    // cout << C << " " << dp[n] << " " << cnt[n] << "\n";
    return {dp[n], cnt[n]};
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> k;
    string s; cin >> s;
    ll 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-k*lo;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 0 ms 2508 KB Output is correct
4 Correct 0 ms 2512 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2508 KB Output is correct
15 Correct 1 ms 2476 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 0 ms 2508 KB Output is correct
4 Correct 0 ms 2512 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2508 KB Output is correct
15 Correct 1 ms 2476 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 2 ms 2904 KB Output is correct
18 Correct 5 ms 2968 KB Output is correct
19 Correct 5 ms 3020 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
21 Correct 5 ms 2908 KB Output is correct
22 Correct 5 ms 2908 KB Output is correct
23 Correct 5 ms 2908 KB Output is correct
24 Correct 4 ms 2772 KB Output is correct
25 Correct 4 ms 2908 KB Output is correct
26 Correct 5 ms 2904 KB Output is correct
27 Correct 5 ms 3020 KB Output is correct
28 Correct 5 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 0 ms 2508 KB Output is correct
4 Correct 0 ms 2512 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2508 KB Output is correct
15 Correct 1 ms 2476 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 2 ms 2904 KB Output is correct
18 Correct 5 ms 2968 KB Output is correct
19 Correct 5 ms 3020 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
21 Correct 5 ms 2908 KB Output is correct
22 Correct 5 ms 2908 KB Output is correct
23 Correct 5 ms 2908 KB Output is correct
24 Correct 4 ms 2772 KB Output is correct
25 Correct 4 ms 2908 KB Output is correct
26 Correct 5 ms 2904 KB Output is correct
27 Correct 5 ms 3020 KB Output is correct
28 Correct 5 ms 2908 KB Output is correct
29 Correct 55 ms 10324 KB Output is correct
30 Correct 56 ms 9088 KB Output is correct
31 Correct 59 ms 8944 KB Output is correct
32 Correct 52 ms 10836 KB Output is correct
33 Correct 55 ms 10324 KB Output is correct
34 Correct 56 ms 10828 KB Output is correct
35 Correct 55 ms 10836 KB Output is correct
36 Correct 56 ms 8852 KB Output is correct
37 Correct 62 ms 10028 KB Output is correct
38 Correct 55 ms 10928 KB Output is correct
39 Correct 56 ms 10180 KB Output is correct
40 Correct 54 ms 10164 KB Output is correct
41 Correct 54 ms 10320 KB Output is correct
42 Correct 24 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 0 ms 2508 KB Output is correct
4 Correct 0 ms 2512 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2508 KB Output is correct
15 Correct 1 ms 2476 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 2 ms 2904 KB Output is correct
18 Correct 5 ms 2968 KB Output is correct
19 Correct 5 ms 3020 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
21 Correct 5 ms 2908 KB Output is correct
22 Correct 5 ms 2908 KB Output is correct
23 Correct 5 ms 2908 KB Output is correct
24 Correct 4 ms 2772 KB Output is correct
25 Correct 4 ms 2908 KB Output is correct
26 Correct 5 ms 2904 KB Output is correct
27 Correct 5 ms 3020 KB Output is correct
28 Correct 5 ms 2908 KB Output is correct
29 Correct 55 ms 10324 KB Output is correct
30 Correct 56 ms 9088 KB Output is correct
31 Correct 59 ms 8944 KB Output is correct
32 Correct 52 ms 10836 KB Output is correct
33 Correct 55 ms 10324 KB Output is correct
34 Correct 56 ms 10828 KB Output is correct
35 Correct 55 ms 10836 KB Output is correct
36 Correct 56 ms 8852 KB Output is correct
37 Correct 62 ms 10028 KB Output is correct
38 Correct 55 ms 10928 KB Output is correct
39 Correct 56 ms 10180 KB Output is correct
40 Correct 54 ms 10164 KB Output is correct
41 Correct 54 ms 10320 KB Output is correct
42 Correct 24 ms 5212 KB Output is correct
43 Correct 810 ms 103112 KB Output is correct
44 Correct 1442 ms 182156 KB Output is correct
45 Correct 1422 ms 179464 KB Output is correct
46 Correct 1333 ms 239160 KB Output is correct
47 Correct 1374 ms 213792 KB Output is correct
48 Correct 1416 ms 235536 KB Output is correct
49 Correct 1418 ms 235588 KB Output is correct
50 Correct 1414 ms 172468 KB Output is correct
51 Correct 1417 ms 169508 KB Output is correct
52 Correct 1414 ms 235532 KB Output is correct
53 Correct 1422 ms 235796 KB Output is correct
54 Correct 1434 ms 187240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 0 ms 2508 KB Output is correct
4 Correct 0 ms 2512 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2508 KB Output is correct
15 Correct 1 ms 2476 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 2 ms 2904 KB Output is correct
18 Correct 5 ms 2968 KB Output is correct
19 Correct 5 ms 3020 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
21 Correct 5 ms 2908 KB Output is correct
22 Correct 5 ms 2908 KB Output is correct
23 Correct 5 ms 2908 KB Output is correct
24 Correct 4 ms 2772 KB Output is correct
25 Correct 4 ms 2908 KB Output is correct
26 Correct 5 ms 2904 KB Output is correct
27 Correct 5 ms 3020 KB Output is correct
28 Correct 5 ms 2908 KB Output is correct
29 Correct 55 ms 10324 KB Output is correct
30 Correct 56 ms 9088 KB Output is correct
31 Correct 59 ms 8944 KB Output is correct
32 Correct 52 ms 10836 KB Output is correct
33 Correct 55 ms 10324 KB Output is correct
34 Correct 56 ms 10828 KB Output is correct
35 Correct 55 ms 10836 KB Output is correct
36 Correct 56 ms 8852 KB Output is correct
37 Correct 62 ms 10028 KB Output is correct
38 Correct 55 ms 10928 KB Output is correct
39 Correct 56 ms 10180 KB Output is correct
40 Correct 54 ms 10164 KB Output is correct
41 Correct 54 ms 10320 KB Output is correct
42 Correct 24 ms 5212 KB Output is correct
43 Correct 810 ms 103112 KB Output is correct
44 Correct 1442 ms 182156 KB Output is correct
45 Correct 1422 ms 179464 KB Output is correct
46 Correct 1333 ms 239160 KB Output is correct
47 Correct 1374 ms 213792 KB Output is correct
48 Correct 1416 ms 235536 KB Output is correct
49 Correct 1418 ms 235588 KB Output is correct
50 Correct 1414 ms 172468 KB Output is correct
51 Correct 1417 ms 169508 KB Output is correct
52 Correct 1414 ms 235532 KB Output is correct
53 Correct 1422 ms 235796 KB Output is correct
54 Correct 1434 ms 187240 KB Output is correct
55 Execution timed out 7059 ms 856352 KB Time limit exceeded
56 Halted 0 ms 0 KB -