제출 #1269444

#제출 시각아이디문제언어결과실행 시간메모리
1269444rayan_bd수열 (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::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<pair<int,int>> dp(n + 2, {inf, 0});
    for (int i = 1; i <= n + 1; i++) dp[i] = {0, i};

    for (int k = 1; k <= K; k++) {
        cht.clear();
        vector<pair<int, int>> ndp(n + 2, {inf, 0});

        for (int j = n; j >= 1; j--) {
            int slope = pref[j];
            int intercept = dp[j+1].first - pref[j] * pref[j];
            cht.add(slope, intercept, j);

            int x = pref[n] + pref[j-1];
            auto [val, idx] = cht.query(x);

            ndp[j] = {-pref[j-1] * pref[n] + val, idx};
        }
        dp.swap(ndp);
    }

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

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