제출 #403926

#제출 시각아이디문제언어결과실행 시간메모리
403926opukittpceno_hhr수열 (APIO14_sequence)C++17
100 / 100
914 ms86968 KiB
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <chrono>
#include <random>
#include <functional>

using namespace std;

typedef long long ll;

const ll INF = 1e18 + 239;
const int N = 1e5 + 7;
const int K = 203;

ll dp[N];
ll ndp[N];

int wr[K][N];

int a[N];
ll ps[N];

vector<int> out;

void rec(int k, int n) {
    if (k == 0) return;
    rec(k - 1, wr[k][n]);
    out.push_back(n - wr[k][n]);
}

namespace CHT {
    typedef long double ld;

    struct Line {
        ll k, b;
        ld start;
        int id;

        Line(ll k_, ll b_, ld start_, int id_) {
            k = k_;
            b = b_;
            start = start_;
            id = id_;
        }

        ld eval_ld(ld x) {
            return k * x + b;
        }
    };

    int pnt = 0;
    vector<Line> st;

    void init() {
        pnt = 0;
        st.clear();
    }

    void add(ll k, ll b, int id) {
        Line cur(k, b, -1, id);
        while (!st.empty()) {
            Line bc = st.back();
            if (cur.eval_ld(bc.start) < bc.eval_ld(bc.start)) {
                st.pop_back();
            } else {
                break;
            }
        }
        if (st.empty()) {
            cur.start = 0;
            st.push_back(cur);
        } else {
            if (st.back().k == cur.k) return;
            ld start = (st.back().b - cur.b) / (cur.k - st.back().k);
            cur.start = start;
            st.push_back(cur);
        }
    }

    ll get(ll x) {
        if (st.empty()) return -1;
        pnt = min(pnt, (int)st.size() - 1);
        while (pnt + 1 < (int)st.size() && st[pnt + 1].start < x) {
            pnt++;
        }
        return st[pnt].id;
    }
}

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

    int n, k;
    cin >> n >> k;
    k++;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        ps[i + 1] = ps[i] + a[i];
    }
    fill(dp, dp + N, INF);
    dp[0] = 0;
    for (int i = 1; i <= k; i++) {
        ndp[0] = INF;
        CHT::init();
        for (int j = 1; j <= n; j++) {
            ndp[j] = INF;
            int add = j - 1;
            if (dp[add] != INF) {
                CHT::add(-ps[add], dp[add] + ps[add] * ps[add], add);
            }
            int p = CHT::get(2 * ps[j]);
            if (p != -1) {
                ndp[j] = dp[p] + ps[p] * ps[p] - 2 * ps[p] * ps[j] + ps[j] * ps[j];
                wr[i][j] = p;
            }
        }
        for (int j = 0; j <= n; j++) {
            dp[j] = ndp[j];
        }
    }
    cout << (ps[n] * ps[n] - dp[n]) / 2 << endl;
    rec(k, n);
    {
        int c = 0;
        for (int i = 0; i + 1 < k; i++) {
            c += out[i];
            cout << c << ' ';
        }
        cout << endl;
    }
}
#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...