Submission #934474

# Submission time Handle Problem Language Result Execution time Memory
934474 2024-02-27T11:45:44 Z boris_mihov Chorus (JOI23_chorus) C++17
0 / 100
6 ms 12892 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)
{
    return prefix[r] - (r + 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;

    if (pos < next[pos])
    {
        dp[pos] = f(next[pos], penalty);
        dp[pos].second++;
    }

    for (int nextPos = next[pos] + 1 ; nextPos <= n + 1 ; ++nextPos)
    {
        std::pair <llong,int> res = f(nextPos, penalty);
        res.first += (next[pos] - pos + 1) * (pos - 1) + pos * (pos - 1) - prefix[next[pos]] + cost(pos, nextPos - 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:117:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  117 |     std::cin >> s + 1;
      |                 ~~^~~
# Verdict Execution time Memory Grader output
1 Runtime error 6 ms 12892 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 6 ms 12892 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 6 ms 12892 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 6 ms 12892 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 6 ms 12892 KB Execution killed with signal 8
2 Halted 0 ms 0 KB -