제출 #1290753

#제출 시각아이디문제언어결과실행 시간메모리
1290753AMel0n수열 (APIO14_sequence)C++20
50 / 100
2094 ms8572 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 -> 1 indexed & splitting just AFTER
*/

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

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++) {
        for(ll m = 1; m <= N; m++) {
            pair<ll,ll> bsf = {-INF, -1};
            for(ll i = 0; i < m; i++) {
                bsf = max(bsf, {dp[k-1][i] + (PS[m]-PS[i]) * (PS[N]-PS[m]), i});
            }
            dp[k][m] = bsf.F;
            nxt[k][m] = bsf.S;
            // cout << k << ' ' << m << ' ' << bsf.F << ' ' << bsf.S << ' ' << dp[k-1][bsf.S] << endl;
        }
    }

    cout << dp[K+1][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...