Submission #1090884

# Submission time Handle Problem Language Result Execution time Memory
1090884 2024-09-19T04:44:45 Z eltu0815 Chorus (JOI23_chorus) C++14
16 / 100
1 ms 464 KB
#include <bits/stdc++.h>
#define MAX 1000005
#define MOD (ll)(1048573)
#define INF (ll)(1e12)
#define inf (int)(1e9)
#define eps (double)(1e-8)
 
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
int tt, N, K;
string str;

int cnt[MAX], pos[MAX], can[MAX], q[MAX], k[MAX];
ll dp[MAX], sum[MAX];

ll cost(int i, int j) {
    return max(sum[j] - sum[pos[i - 1] - 1] - (ll)(j - pos[i - 1] + 1) * (i - 1), 0ll);
}

ll func(int i, int j) {
    return dp[i] + cost(i + 1, j);
}

int cross(int i, int j) {
    int lo = j , hi = N + 1;
    while(lo + 1 < hi) {
        int mid = (lo + hi) >> 1;
        if(func(i, mid) <= func(j, mid)) lo = mid;
        else hi = mid;
    }
    return lo; 
}

void monotoneQueue(ll lambda) {
    int str = 0, ed = 1;
    q[0] = 0; can[0] = N;
    for(int i = 1; i <= N; ++i) {
        while(can[str] < i) ++str;
        dp[i] = func(q[str], i) + lambda; k[i] = k[q[str]] + 1;
        
        while(str + 1 < ed && can[ed - 2] >= cross(q[ed - 1], i)) --ed;
        can[ed - 1] = cross(q[ed - 1], i); q[ed] = i; can[ed] = N;
        ++ed;
    }
    
//    cout << lambda << ' ' << k[N] << endl;
//    for(int i = 1; i <= N; ++i) cout << k[i] << ' ';
//    cout << endl;
//    for(int i = 1; i <= N; ++i) cout << dp[i] - k[i] * lambda << ' ';
//    cout << endl;
//    cout << endl;

    return;
}

ll alienTrick() {
    ll lo = 0, hi = INF;
    ll ans = 0;
    while(lo + 1 < hi) {
        ll mid = (lo + hi) >> 1;
        monotoneQueue(mid);
        
        ans = max(ans, dp[N] - mid * K);
        
        if(k[N] >= K) lo = mid;
        else hi = mid;
    }
    
    return ans;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    
    cin >> N >> K >> str;
    for(int i = 0, tmp = 0, p = 0; i < 2 * N; ++i) {
        if(str[i] == 'A') cnt[++p] = tmp;
        else ++tmp;
    }
    
    for(int i = 0, j = 1; i <= N; ++i) {
        while(j <= N && cnt[j] < i) ++j;
        pos[i] = max(i + 1, j);
    }
    
    for(int i = 1; i <= N; ++i) sum[i] = sum[i - 1] + cnt[i];
    
    cout << alienTrick();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 464 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 464 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Incorrect 1 ms 344 KB Output isn't correct
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 464 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Incorrect 1 ms 344 KB Output isn't correct
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 464 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Incorrect 1 ms 344 KB Output isn't correct
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 464 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Incorrect 1 ms 344 KB Output isn't correct
18 Halted 0 ms 0 KB -