답안 #902511

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
902511 2024-01-10T14:09:58 Z Tuanlinh123 Chorus (JOI23_chorus) C++17
87 / 100
7000 ms 818424 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=1e6+5, maxn=1e6+5;
long long cnt[maxn], to[maxn];
long long n, k, dp[maxn], pre[maxn];
 
struct Line
{
    long long a=0, b=inf*inf;
    ll idx=0;
 
    Line(){}
    Line(long long a, long long b, ll idx): a(a), b(b), idx(idx){}
    inline pair<long long, ll> ask(ll x){return {a*x+b, idx};}
};
 
struct Node
{
    Line A;
    ll l=0, r=0;
};
Node LC[maxn*25];

struct Lichao
{
    ll crr;
    Lichao()
    {
        crr=0;
        LC[0]=Node();
    }
    inline void addline(Line A)
    {
        ll Start=-inf, End=inf, i=0;
        while (1)
        {
            ll mid=(Start+End)/2;
            bool l=A.ask(Start).fi<LC[i].A.ask(Start).fi;
            bool m=A.ask(mid).fi<LC[i].A.ask(mid).fi;
            if (m) swap(A, LC[i].A);
            if (A.b==inf*inf) break;
            if (Start+1!=End)
            {
                if (l!=m)
                {
                    if (!LC[i].l)
                    {
                        LC[i].l=++crr;
                        LC[crr]=Node();
                    }
                    i=LC[i].l;
                    End=mid;
                }
                else
                {
                    if (!LC[i].r)
                    {
                        LC[i].r=++crr;
                        LC[crr]=Node();
                    }
                    i=LC[i].r;
                    Start=mid;
                }
            }
            else break;
        }
    }
 
    inline pair<long long, ll> query(ll x)
    {
        ll Start=-inf, End=inf, i=0;
        pair<long long, ll> ans={inf*inf, 0};
        while (1)
        {
            ll mid=(Start+End)/2;
            ans=min(ans, LC[i].A.ask(x));
            if (x<mid && LC[i].l)
            {
                i=LC[i].l;
                End=mid;
            }
            else if (x>=mid && LC[i].r)
            {
                i=LC[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));
    for (ll i=1; i<=n; i++)
        dp[i]=inf*inf, cnt[i]=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;
    }
    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;
    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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 234 ms 783156 KB Output is correct
2 Correct 239 ms 786464 KB Output is correct
3 Correct 185 ms 783196 KB Output is correct
4 Correct 227 ms 783308 KB Output is correct
5 Correct 202 ms 783140 KB Output is correct
6 Correct 202 ms 783144 KB Output is correct
7 Correct 196 ms 783224 KB Output is correct
8 Correct 194 ms 783044 KB Output is correct
9 Correct 190 ms 783184 KB Output is correct
10 Correct 195 ms 783184 KB Output is correct
11 Correct 185 ms 783152 KB Output is correct
12 Correct 193 ms 783112 KB Output is correct
13 Correct 195 ms 783072 KB Output is correct
14 Correct 205 ms 783164 KB Output is correct
15 Correct 208 ms 782996 KB Output is correct
16 Correct 208 ms 783044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 234 ms 783156 KB Output is correct
2 Correct 239 ms 786464 KB Output is correct
3 Correct 185 ms 783196 KB Output is correct
4 Correct 227 ms 783308 KB Output is correct
5 Correct 202 ms 783140 KB Output is correct
6 Correct 202 ms 783144 KB Output is correct
7 Correct 196 ms 783224 KB Output is correct
8 Correct 194 ms 783044 KB Output is correct
9 Correct 190 ms 783184 KB Output is correct
10 Correct 195 ms 783184 KB Output is correct
11 Correct 185 ms 783152 KB Output is correct
12 Correct 193 ms 783112 KB Output is correct
13 Correct 195 ms 783072 KB Output is correct
14 Correct 205 ms 783164 KB Output is correct
15 Correct 208 ms 782996 KB Output is correct
16 Correct 208 ms 783044 KB Output is correct
17 Correct 189 ms 783192 KB Output is correct
18 Correct 189 ms 783136 KB Output is correct
19 Correct 193 ms 783032 KB Output is correct
20 Correct 216 ms 783260 KB Output is correct
21 Correct 243 ms 783352 KB Output is correct
22 Correct 213 ms 783220 KB Output is correct
23 Correct 235 ms 783024 KB Output is correct
24 Correct 198 ms 783184 KB Output is correct
25 Correct 200 ms 783020 KB Output is correct
26 Correct 205 ms 783020 KB Output is correct
27 Correct 209 ms 783028 KB Output is correct
28 Correct 203 ms 783184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 234 ms 783156 KB Output is correct
2 Correct 239 ms 786464 KB Output is correct
3 Correct 185 ms 783196 KB Output is correct
4 Correct 227 ms 783308 KB Output is correct
5 Correct 202 ms 783140 KB Output is correct
6 Correct 202 ms 783144 KB Output is correct
7 Correct 196 ms 783224 KB Output is correct
8 Correct 194 ms 783044 KB Output is correct
9 Correct 190 ms 783184 KB Output is correct
10 Correct 195 ms 783184 KB Output is correct
11 Correct 185 ms 783152 KB Output is correct
12 Correct 193 ms 783112 KB Output is correct
13 Correct 195 ms 783072 KB Output is correct
14 Correct 205 ms 783164 KB Output is correct
15 Correct 208 ms 782996 KB Output is correct
16 Correct 208 ms 783044 KB Output is correct
17 Correct 189 ms 783192 KB Output is correct
18 Correct 189 ms 783136 KB Output is correct
19 Correct 193 ms 783032 KB Output is correct
20 Correct 216 ms 783260 KB Output is correct
21 Correct 243 ms 783352 KB Output is correct
22 Correct 213 ms 783220 KB Output is correct
23 Correct 235 ms 783024 KB Output is correct
24 Correct 198 ms 783184 KB Output is correct
25 Correct 200 ms 783020 KB Output is correct
26 Correct 205 ms 783020 KB Output is correct
27 Correct 209 ms 783028 KB Output is correct
28 Correct 203 ms 783184 KB Output is correct
29 Correct 217 ms 783184 KB Output is correct
30 Correct 227 ms 783444 KB Output is correct
31 Correct 217 ms 783436 KB Output is correct
32 Correct 218 ms 783184 KB Output is correct
33 Correct 230 ms 783292 KB Output is correct
34 Correct 231 ms 783188 KB Output is correct
35 Correct 221 ms 783188 KB Output is correct
36 Correct 223 ms 783240 KB Output is correct
37 Correct 215 ms 783232 KB Output is correct
38 Correct 212 ms 783284 KB Output is correct
39 Correct 220 ms 783424 KB Output is correct
40 Correct 217 ms 783368 KB Output is correct
41 Correct 223 ms 783212 KB Output is correct
42 Correct 208 ms 783360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 234 ms 783156 KB Output is correct
2 Correct 239 ms 786464 KB Output is correct
3 Correct 185 ms 783196 KB Output is correct
4 Correct 227 ms 783308 KB Output is correct
5 Correct 202 ms 783140 KB Output is correct
6 Correct 202 ms 783144 KB Output is correct
7 Correct 196 ms 783224 KB Output is correct
8 Correct 194 ms 783044 KB Output is correct
9 Correct 190 ms 783184 KB Output is correct
10 Correct 195 ms 783184 KB Output is correct
11 Correct 185 ms 783152 KB Output is correct
12 Correct 193 ms 783112 KB Output is correct
13 Correct 195 ms 783072 KB Output is correct
14 Correct 205 ms 783164 KB Output is correct
15 Correct 208 ms 782996 KB Output is correct
16 Correct 208 ms 783044 KB Output is correct
17 Correct 189 ms 783192 KB Output is correct
18 Correct 189 ms 783136 KB Output is correct
19 Correct 193 ms 783032 KB Output is correct
20 Correct 216 ms 783260 KB Output is correct
21 Correct 243 ms 783352 KB Output is correct
22 Correct 213 ms 783220 KB Output is correct
23 Correct 235 ms 783024 KB Output is correct
24 Correct 198 ms 783184 KB Output is correct
25 Correct 200 ms 783020 KB Output is correct
26 Correct 205 ms 783020 KB Output is correct
27 Correct 209 ms 783028 KB Output is correct
28 Correct 203 ms 783184 KB Output is correct
29 Correct 217 ms 783184 KB Output is correct
30 Correct 227 ms 783444 KB Output is correct
31 Correct 217 ms 783436 KB Output is correct
32 Correct 218 ms 783184 KB Output is correct
33 Correct 230 ms 783292 KB Output is correct
34 Correct 231 ms 783188 KB Output is correct
35 Correct 221 ms 783188 KB Output is correct
36 Correct 223 ms 783240 KB Output is correct
37 Correct 215 ms 783232 KB Output is correct
38 Correct 212 ms 783284 KB Output is correct
39 Correct 220 ms 783424 KB Output is correct
40 Correct 217 ms 783368 KB Output is correct
41 Correct 223 ms 783212 KB Output is correct
42 Correct 208 ms 783360 KB Output is correct
43 Correct 574 ms 785476 KB Output is correct
44 Correct 915 ms 786712 KB Output is correct
45 Correct 842 ms 786772 KB Output is correct
46 Correct 789 ms 786748 KB Output is correct
47 Correct 802 ms 786740 KB Output is correct
48 Correct 839 ms 786808 KB Output is correct
49 Correct 841 ms 786708 KB Output is correct
50 Correct 857 ms 786652 KB Output is correct
51 Correct 902 ms 786708 KB Output is correct
52 Correct 861 ms 786704 KB Output is correct
53 Correct 860 ms 786872 KB Output is correct
54 Correct 883 ms 786900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 234 ms 783156 KB Output is correct
2 Correct 239 ms 786464 KB Output is correct
3 Correct 185 ms 783196 KB Output is correct
4 Correct 227 ms 783308 KB Output is correct
5 Correct 202 ms 783140 KB Output is correct
6 Correct 202 ms 783144 KB Output is correct
7 Correct 196 ms 783224 KB Output is correct
8 Correct 194 ms 783044 KB Output is correct
9 Correct 190 ms 783184 KB Output is correct
10 Correct 195 ms 783184 KB Output is correct
11 Correct 185 ms 783152 KB Output is correct
12 Correct 193 ms 783112 KB Output is correct
13 Correct 195 ms 783072 KB Output is correct
14 Correct 205 ms 783164 KB Output is correct
15 Correct 208 ms 782996 KB Output is correct
16 Correct 208 ms 783044 KB Output is correct
17 Correct 189 ms 783192 KB Output is correct
18 Correct 189 ms 783136 KB Output is correct
19 Correct 193 ms 783032 KB Output is correct
20 Correct 216 ms 783260 KB Output is correct
21 Correct 243 ms 783352 KB Output is correct
22 Correct 213 ms 783220 KB Output is correct
23 Correct 235 ms 783024 KB Output is correct
24 Correct 198 ms 783184 KB Output is correct
25 Correct 200 ms 783020 KB Output is correct
26 Correct 205 ms 783020 KB Output is correct
27 Correct 209 ms 783028 KB Output is correct
28 Correct 203 ms 783184 KB Output is correct
29 Correct 217 ms 783184 KB Output is correct
30 Correct 227 ms 783444 KB Output is correct
31 Correct 217 ms 783436 KB Output is correct
32 Correct 218 ms 783184 KB Output is correct
33 Correct 230 ms 783292 KB Output is correct
34 Correct 231 ms 783188 KB Output is correct
35 Correct 221 ms 783188 KB Output is correct
36 Correct 223 ms 783240 KB Output is correct
37 Correct 215 ms 783232 KB Output is correct
38 Correct 212 ms 783284 KB Output is correct
39 Correct 220 ms 783424 KB Output is correct
40 Correct 217 ms 783368 KB Output is correct
41 Correct 223 ms 783212 KB Output is correct
42 Correct 208 ms 783360 KB Output is correct
43 Correct 574 ms 785476 KB Output is correct
44 Correct 915 ms 786712 KB Output is correct
45 Correct 842 ms 786772 KB Output is correct
46 Correct 789 ms 786748 KB Output is correct
47 Correct 802 ms 786740 KB Output is correct
48 Correct 839 ms 786808 KB Output is correct
49 Correct 841 ms 786708 KB Output is correct
50 Correct 857 ms 786652 KB Output is correct
51 Correct 902 ms 786708 KB Output is correct
52 Correct 861 ms 786704 KB Output is correct
53 Correct 860 ms 786872 KB Output is correct
54 Correct 883 ms 786900 KB Output is correct
55 Correct 5112 ms 805844 KB Output is correct
56 Execution timed out 7028 ms 818424 KB Time limit exceeded
57 Halted 0 ms 0 KB -