제출 #1290757

#제출 시각아이디문제언어결과실행 시간메모리
1290757AMel0nSplit the sequence (APIO14_sequence)C++20
100 / 100
800 ms81416 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define FOR(i,N) for(ll i = 0; i < N; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second

// APIO 14 Split The Sequence
/*
(0 indexed)
dp[k][m] = max score if we made k splits and the kth split was just BEFORE index m 
dp[k][m] = max of: dp[k-1][i] + (ps[m]-p[i]) * (ps[N]-p[m]) for all (0 <= i < m) 
nxt[k][m] = i where i was the optimal preceeding split point for split point m
0 indexed & splitting just BEFORE (code format) -> 1 indexed & splitting just AFTER (output format)
*/

const ll INF = 1e18;
const ll MAXN = 1e5 + 5;
const ll MAXK = 205;
ll N, K, PS[MAXN], dp[2][MAXN];
int nxt[MAXK][MAXN];


void solve(ll l, ll r, ll lOpt, ll rOpt, ll k) {
    if (l > r) return ;
    ll m = (l + r) / 2;
    pair<ll,ll> bsf = {-INF, -1};
    for(ll i = lOpt; i <= min(m-1, rOpt); i++) {
        bsf = max(bsf, {dp[(k-1)%2][i] + (PS[m]-PS[i]) * (PS[N]-PS[m]), i});
    }
    dp[k%2][m] = bsf.F;
    nxt[k][m] = bsf.S;
    // cout << k << ' ' << m << ' ' << bsf.F << ' ' << bsf.S << ' ' << dp[k-1][bsf.S] << endl;
    solve(l, m-1, lOpt, bsf.S, k);
    solve(m+1, r, bsf.S, rOpt, k);
}

signed main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N >> K;
    FOR(i, N) {
        cin >> PS[i+1];
        PS[i+1] += PS[i];
    }
    for(ll k = 1; k <= K+1; k++) {
        solve(1, N, 0, N-1, k);
    }

    cout << dp[(K+1)%2][N] << endl;
    ll idx = N;
    for(ll k = K+1; k > 1; k--) {
        cout << nxt[k][idx] << ' ';
        idx = nxt[k][idx];
    }
}
#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...