제출 #1356015

#제출 시각아이디문제언어결과실행 시간메모리
1356015alexddChorus (JOI23_chorus)C++20
100 / 100
1454 ms42044 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int INF = 1e18;
const int MAXN = 1e6 + 5;

typedef long double ld;
struct line
{
    int m, b;
    int eval(int x) {return m * x + b;}
};
ld interx(line a, line b)
{
    return ((ld)b.b - (ld)a.b) / ((ld)a.m - (ld)b.m);
}

int n,k;
string s;
int pozb[MAXN];
int dp[MAXN], cnt_songs[MAXN];
int auxdp[MAXN];
pair<int, int> solve(int coef)//{{rez_coef, {nr_swaps, cnt_songs}}
{
    int lim = 0, balance = 0, balance_hull = 0;
    deque<line> hull;

    deque<int> late;

    dp[0] = 0;
    cnt_songs[0] = 0;
    for(int i=1;i<=n;i++)
    {

        dp[i] = INF;
        cnt_songs[i] = -1;

        auxdp[i-1] = dp[i-1] + coef - balance;
        balance += max(0LL, (i-1) - pozb[i]);

        while(!late.empty() && auxdp[i-1] <= auxdp[late.back()])
            late.pop_back();
        late.push_back(i-1);

        while(lim + 1 <= i && pozb[lim + 1] < i)
        {
            line cur = {-lim, auxdp[lim] + lim * (i-1) - balance_hull};
            while(hull.size() >= 2 && interx(hull[(int)hull.size()-2], hull.back()) >= interx(hull.back(), cur))
                hull.pop_back();
            hull.push_back(cur);
            lim++;
        }

        while(!late.empty() && late.front() < lim)
            late.pop_front();

        balance_hull += lim;

        if(!late.empty())
        {
            int x = late.front();
            if(auxdp[x] + balance < dp[i])
            {
                dp[i] = auxdp[x] + balance;
                cnt_songs[i] = cnt_songs[x] + 1;
            }
        }

        while(hull.size() >= 2 && hull[0].eval(i) >= hull[1].eval(i))
            hull.pop_front();
        if(!hull.empty())
        {
            line l = hull[0];
            int cv = l.eval(i) + balance + balance_hull;
            if(cv < dp[i])
            {
                dp[i] = cv;
                cnt_songs[i] = cnt_songs[-l.m] + 1;
            }
        }
    }

    return {dp[n], cnt_songs[n]};
}

signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n>>k>>s;
    assert(s.size() == 2 * n);
    int cnta = 0, cntb = 0;
    for(int i=0;i<2*n;i++)
    {
        if(s[i] == 'A')
        {
            cnta++;
        }
        else
        {
            cntb++;
            pozb[cntb] = cnta;
        }
    }
    assert(cnta == n && cntb == n);

    int st = 0, dr = 1e12, ans = -1;
    while(st <= dr)
    {
        int mij = (st + dr) / 2;
        auto x = solve(mij);

        if(x.second >= k)
        {
            ans = mij;
            st = mij + 1;
        }
        else
        {
            dr = mij - 1;
        }
    }
    assert(ans != -1);

    auto x = solve(ans);
    cout<<x.first - k * ans;
    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…