답안 #934471

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
934471 2024-02-27T11:36:59 Z boris_mihov Chorus (JOI23_chorus) C++17
61 / 100
7000 ms 15964 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

typedef long long llong;
const int MAXN = 1000000 + 10;
const llong INF = 1e18;

int n, k;
int h[MAXN];
int next[MAXN];
llong prefix[MAXN];
std::pair <llong,int> dp[MAXN];
int bl[MAXN];
char s[2 * MAXN];

llong cost(int l, int r)
{
    if (next[l] > r)
    {
        return 0;
    }

    return (next[l] - l + 1) * (l - 1) + prefix[r] - prefix[next[l]] - (r - l + 1) * (l - 1);
}

int timer;
std::pair <llong,int> f(int pos, llong penalty)
{
    if (pos == n + 1)
    {
        return {0, 0};
    }

    if (bl[pos] == timer)
    {
        return dp[pos];
    }

    dp[pos] = {INF, 0};
    bl[pos] = timer;

    for (int next = pos + 1 ; next <= n + 1 ; ++next)
    {
        std::pair <llong,int> res = f(next, penalty);
        res.first += cost(pos, next - 1) + penalty;
        res.second += 1;

        dp[pos] = std::min(dp[pos], res);
    }

    return dp[pos];
}

void solve()
{
    int cntA = 0;
    int cntB = 0;
    for (int i = 1 ; i <= 2 * n ; ++i)
    {
        if (s[i] == 'B')
        {
            cntB++;
        } else
        {
            cntA++;
            h[cntA] = cntB;
        }
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        prefix[i] = prefix[i - 1] + h[i];
    }

    next[n + 1] = n + 1;
    h[n + 1] = n + 1;
    for (int i = n ; i >= 1 ; --i)
    {
        next[i] = next[i + 1];
        while (next[i] >= i && h[next[i]] >= i)
        {
            next[i]--;
        }
    }

    llong l = -1, r = 1LL * n * n + 1, mid;
    while (l < r - 1)
    {
        timer++;
        mid = (l + r) / 2;
        if (f(1, mid).second > k) l = mid;
        else r = mid;
    }

    timer++;
    auto [fL, groupsL] = f(1, l); timer++;
    auto [fR, groupsR] = f(1, r); 
    fL -= groupsL * l;
    fR -= groupsR * r;

    if (groupsR == k)
    {
        std::cout << fR << '\n';
        return;
    }

    std::cout << fL + (fR - fL) * (groupsL - k) / (groupsL - groupsR) << '\n';
}

void input()
{
    std::cin >> n >> k;
    std::cin >> s + 1;
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}

Compilation message

chorus.cpp: In function 'void input()':
chorus.cpp:116:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  116 |     std::cin >> s + 1;
      |                 ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 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 6492 KB Output is correct
7 Correct 2 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 6492 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 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 6492 KB Output is correct
7 Correct 2 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 6492 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 3 ms 6488 KB Output is correct
18 Correct 16 ms 6492 KB Output is correct
19 Correct 16 ms 6492 KB Output is correct
20 Correct 20 ms 6492 KB Output is correct
21 Correct 16 ms 6492 KB Output is correct
22 Correct 16 ms 6492 KB Output is correct
23 Correct 16 ms 6492 KB Output is correct
24 Correct 17 ms 6696 KB Output is correct
25 Correct 17 ms 6492 KB Output is correct
26 Correct 20 ms 6492 KB Output is correct
27 Correct 17 ms 6492 KB Output is correct
28 Correct 17 ms 6488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 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 6492 KB Output is correct
7 Correct 2 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 6492 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 3 ms 6488 KB Output is correct
18 Correct 16 ms 6492 KB Output is correct
19 Correct 16 ms 6492 KB Output is correct
20 Correct 20 ms 6492 KB Output is correct
21 Correct 16 ms 6492 KB Output is correct
22 Correct 16 ms 6492 KB Output is correct
23 Correct 16 ms 6492 KB Output is correct
24 Correct 17 ms 6696 KB Output is correct
25 Correct 17 ms 6492 KB Output is correct
26 Correct 20 ms 6492 KB Output is correct
27 Correct 17 ms 6492 KB Output is correct
28 Correct 17 ms 6488 KB Output is correct
29 Correct 2004 ms 7212 KB Output is correct
30 Correct 1989 ms 7260 KB Output is correct
31 Correct 2003 ms 7260 KB Output is correct
32 Correct 1982 ms 7256 KB Output is correct
33 Correct 1939 ms 7252 KB Output is correct
34 Correct 1979 ms 7260 KB Output is correct
35 Correct 1994 ms 7260 KB Output is correct
36 Correct 1983 ms 7256 KB Output is correct
37 Correct 2088 ms 7260 KB Output is correct
38 Correct 1988 ms 7236 KB Output is correct
39 Correct 1990 ms 7260 KB Output is correct
40 Correct 1993 ms 7260 KB Output is correct
41 Correct 2008 ms 7236 KB Output is correct
42 Correct 1885 ms 7232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 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 6492 KB Output is correct
7 Correct 2 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 6492 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 3 ms 6488 KB Output is correct
18 Correct 16 ms 6492 KB Output is correct
19 Correct 16 ms 6492 KB Output is correct
20 Correct 20 ms 6492 KB Output is correct
21 Correct 16 ms 6492 KB Output is correct
22 Correct 16 ms 6492 KB Output is correct
23 Correct 16 ms 6492 KB Output is correct
24 Correct 17 ms 6696 KB Output is correct
25 Correct 17 ms 6492 KB Output is correct
26 Correct 20 ms 6492 KB Output is correct
27 Correct 17 ms 6492 KB Output is correct
28 Correct 17 ms 6488 KB Output is correct
29 Correct 2004 ms 7212 KB Output is correct
30 Correct 1989 ms 7260 KB Output is correct
31 Correct 2003 ms 7260 KB Output is correct
32 Correct 1982 ms 7256 KB Output is correct
33 Correct 1939 ms 7252 KB Output is correct
34 Correct 1979 ms 7260 KB Output is correct
35 Correct 1994 ms 7260 KB Output is correct
36 Correct 1983 ms 7256 KB Output is correct
37 Correct 2088 ms 7260 KB Output is correct
38 Correct 1988 ms 7236 KB Output is correct
39 Correct 1990 ms 7260 KB Output is correct
40 Correct 1993 ms 7260 KB Output is correct
41 Correct 2008 ms 7236 KB Output is correct
42 Correct 1885 ms 7232 KB Output is correct
43 Execution timed out 7031 ms 15964 KB Time limit exceeded
44 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 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 6492 KB Output is correct
7 Correct 2 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 6492 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 3 ms 6488 KB Output is correct
18 Correct 16 ms 6492 KB Output is correct
19 Correct 16 ms 6492 KB Output is correct
20 Correct 20 ms 6492 KB Output is correct
21 Correct 16 ms 6492 KB Output is correct
22 Correct 16 ms 6492 KB Output is correct
23 Correct 16 ms 6492 KB Output is correct
24 Correct 17 ms 6696 KB Output is correct
25 Correct 17 ms 6492 KB Output is correct
26 Correct 20 ms 6492 KB Output is correct
27 Correct 17 ms 6492 KB Output is correct
28 Correct 17 ms 6488 KB Output is correct
29 Correct 2004 ms 7212 KB Output is correct
30 Correct 1989 ms 7260 KB Output is correct
31 Correct 2003 ms 7260 KB Output is correct
32 Correct 1982 ms 7256 KB Output is correct
33 Correct 1939 ms 7252 KB Output is correct
34 Correct 1979 ms 7260 KB Output is correct
35 Correct 1994 ms 7260 KB Output is correct
36 Correct 1983 ms 7256 KB Output is correct
37 Correct 2088 ms 7260 KB Output is correct
38 Correct 1988 ms 7236 KB Output is correct
39 Correct 1990 ms 7260 KB Output is correct
40 Correct 1993 ms 7260 KB Output is correct
41 Correct 2008 ms 7236 KB Output is correct
42 Correct 1885 ms 7232 KB Output is correct
43 Execution timed out 7031 ms 15964 KB Time limit exceeded
44 Halted 0 ms 0 KB -