Submission #113078

#TimeUsernameProblemLanguageResultExecution timeMemory
113078dolphingarlicSplit the sequence (APIO14_sequence)C++14
71 / 100
271 ms131072 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define FOR(i, x, y) for (ll i = x; i < y; i++)
typedef long long ll;
using namespace std;

ll n, k;
pair<ll, ll> dp[201][100001];
ll pref[100001]{0};

bool case1(ll x, ll y, ll i, ll indx) {
    return (dp[indx][y].first - dp[indx][x].first >=
            (pref[y] - pref[x]) * (pref[n] - pref[i]));
}

bool case2(ll x, ll y, ll i, ll indx) {
    return ((dp[indx][y].first - dp[indx][x].first) * (pref[i] - pref[y]) <=
            (dp[indx][i].first - dp[indx][y].first) * (pref[y] - pref[x]));
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    FOR(i, 1, n + 1) {
        ll x;
        cin >> x;
        pref[i] = pref[i - 1] + x;
    }

    fill(dp[0], dp[0] + n + 1, make_pair(0, 0));
    FOR(i, 1, k + 1) {
        deque<ll> q;
        q.push_back(0);
        dp[i][0] = {0, 0};
        FOR(j, 1, n + 1) {
            while (q.size() > 1 && case1(q[0], q[1], j, i - 1)) q.pop_front();

            ll x = q[0];
            dp[i][j] = {dp[i - 1][x].first + (pref[j] - pref[x]) * (pref[n] - pref[j]), x};

            while (q.size() > 1 && case2(q[q.size() - 2], q[q.size() - 1], j, i - 1)) q.pop_back();
            q.push_back(j);
        }
    }

    ll mx = -1, indx = -1;
    FOR(i, 1, n + 1) {
        if (dp[k][i].first > mx) mx = dp[k][i].first, indx = i;
    }
    cout << mx << '\n';
    FOR(i, 0, k) {
        cout << indx << ' ';
        indx = dp[k - i][indx].second;
    }
    cout << '\n';
    return 0;
}
#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...