Submission #1269436

#TimeUsernameProblemLanguageResultExecution timeMemory
1269436rayan_bdSplit the sequence (APIO14_sequence)C++20
0 / 100
0 ms324 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int inf = -1e18;

struct Line {
    mutable int k, m, p, idx;
    bool operator<(const Line& o) const { return k < o.k; }
    bool operator<(int x) const { return p < x; }
};

struct LineContainer : multiset<Line, less<>> {
    static const int inf = LLONG_MAX;
    int div(int a, int b) { return a / b - ((a ^ b) < 0 && a % b); }
    bool isect(iterator x, iterator y) {
        if (y == end()) { x->p = inf; return false; }
        if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
        else x->p = div(y->m - x->m, x->k - y->k);
        return x->p >= y->p;
    }
    void add(int k, int m, int idx) {
        auto z = insert({k, m, 0, idx}), y = z++, x = y;
        while (isect(y, z)) z = erase(z);
        if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
        while ((y = x) != begin() && (--x)->p >= y->p)
            isect(x, erase(y));
    }
    pair<int,int> query(int x) {
        assert(!empty());
        auto l = *lower_bound(x);
        return {l.k * x + l.m, l.idx};
    }
} cht;


signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    int n, K;
    cin >> n >> K;
    vector<int> ar(n + 1), pref(n + 1);
    for(int i = 1; i <= n; ++i){
        cin >> ar[i];
        pref[i] = pref[i - 1] + ar[i];
    }

    vector<vector<pair<int, int>>> dp(n + 5, vector<pair<int, int>>(2, {inf, 0}));

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

    for (int k = 1; k <= K; k++) {
        cht.clear();
        for (int j = n; j >= 1; j--) {
            int slope = pref[j];
            int intercept = dp[j+1][1 - (k & 1)].first - pref[j] * pref[j];
            cht.add(slope, intercept, j);
            int x = pref[n] + pref[j-1];
            auto [val, idx] = cht.query(x);
            dp[j][k & 1] = {-pref[j-1] * pref[n] + val, idx};
        }
    }

    cout << dp[1][K & 1].first << "\n";

    int i = 1, k = K;
    while (k > 0 && i <= n) {
        int j = dp[i][k & 1].second;
        if (j == -1) break;
        cout << j << " ";
        i = j + 1;
        k--;
    }
    cout << "\n";
}
#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...