제출 #498670

#제출 시각아이디문제언어결과실행 시간메모리
498670StickfishSplit the sequence (APIO14_sequence)C++17
0 / 100
8 ms372 KiB
#include <iostream>
using namespace std;
using ll = long long;

const ll INF = 4e18;
const ll MAXN = 100008;
const ll MAXK = 208;
ll a[MAXN];
ll psm_[MAXN];
ll psqsm_[MAXN];
ll* psm = psm_ + 1;
ll* psqsm = psqsm_ + 1;
ll dp_[MAXK][MAXN];
ll* dp[MAXK];
ll opt_[MAXK][MAXN];
ll* opt[MAXK];

void get_layer(ll t, ll l, ll r, ll pl, ll pr) {
    if (r - l <= 0)
        return;
    ll m = (l + r) / 2;
    if (m < 0 || m >= MAXN || t == 0)
        return;
    opt[t][m] = pl;
    dp[t][m] = INF;
    for (ll i = pl; i <= pr && i < m; ++i) {
        if (i < 0 || i >= MAXN)
            return;
        ll sqsm = psqsm[m] - psqsm[i];
        ll sm = psm[m] - psm[i];
        if (dp[t][m] > dp[t - 1][i] + (sm * sm - sqsm) / 2) {
            dp[t][m] = dp[t - 1][i] + (sm * sm - sqsm) / 2;
            opt[t][m] = i;
        }
    }
    get_layer(t, l, m, pl, opt[t][m]);
    get_layer(t, m + 1, r, opt[t][m], pr);
}

signed main() {
    ll n, k;
    cin >> n >> k;
    dp[0][-2] = 6;
    ++k;
    if (n >= MAXN || k >= MAXK || k > n)
        return 0;
    for (ll i = 0; i < n; ++i)
        cin >> a[i];
    for (ll i = 0; i < k; ++i) {
        dp[i] = dp_[i] + 1;
        opt[i] = opt_[i] + 1;
    }
    for (ll i = 0; i < n; ++i) {
        psm[i] = psm[i - 1] + a[i];
        psqsm[i] = psqsm[i - 1] + a[i] * a[i];
        dp[0][i] = (psm[i] * psm[i] - psqsm[i]) / 2;
        opt[0][i] = -1;
    }
    for (ll t = 1; t < k; ++t) {
        get_layer(t, t, n, t - 1, n - 1);
    }
    if (n >= MAXN || k >= MAXK || n == 0 || k == 0)
        return 0;
    cout << (psm[n - 1] * psm[n - 1] - psqsm[n - 1]) / 2 - dp[k - 1][n - 1] << endl;
    ll i = n - 1;
    ll t = k;
    while (--t) {
        i = opt[t][i];
        if (i != -1) {
            cout << i + 1 << ' ';
        }
    }
    cout << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...