55592023-07-26 14:18:14szilMajomházcpp17Elfogadva 100/1001.889s23600 KiB
#include <bits/stdc++.h>

using ll = long long;
using namespace std;

const int MAXN = 200002;
const ll INF = 1e18;

int n, k;
ll a[MAXN], pref[MAXN], dp[MAXN], used[MAXN];

struct Point {
    ll a = -1;

    Point() {}

    Point(int x) {
        a = x;
    }

    ll f(ll b) {
        if (a == -1) return INF;
        return (pref[b] - pref[a]) * (b - a) + dp[a];
    }

    bool operator<(const Point &x) const {
        return true;
    }
};

Point tree[4*MAXN];

void add(Point p, int v = 1, int tl = 0, int tr = n+1) {
    int tm = (tl + tr) / 2;
    bool lef = p.f(tl) < tree[v].f(tl);
    bool mid = p.f(tm) < tree[v].f(tm);
    if (mid) swap(p, tree[v]);

    if (tl + 1 != tr) {
        if (lef != mid) {
            add(p, v*2, tl, tm);
        } else {
            add(p, v*2+1, tm, tr);
        }
    }
}

pair<ll, Point> get(int b, int v = 1, int tl = 0, int tr = n+1) {
    int tm = (tl + tr) / 2;
    pair<ll, Point> x = {tree[v].f(b), tree[v]};
    if (tl + 1 == tr) {
        return x;
    } else {
        if (b < tm) {
            return min(x, get(b, v*2, tl, tm));
        } else {
            return min(x, get(b, v*2+1, tm, tr));
        }
    }
}

pair<ll, ll> solve(ll lambda) {
    fill(tree, tree+4*MAXN, Point(-1));
    add(Point(0));
    for (int i = 1; i <= n; i++) {
        auto opt = get(i);
        dp[i] = opt.first + lambda;
        used[i] = used[opt.second.a] + 1;
        add(Point(i));
    }
    return {dp[n], used[n]};
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> k; k++;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        pref[i] = a[i] + pref[i-1];
    }
    ll lo = 0, hi = 1e17;
    while (lo < hi) {
        ll lambda = (lo + hi) / 2;
        auto x = solve(lambda);
        if (x.second <= k) hi = lambda;
        else lo = lambda + 1;
    }
    auto ans = solve(lo);
    cout << (ans.first - lo * ans.second) << "\n";
    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva27ms14424 KiB
2Elfogadva87ms15052 KiB
subtask210/10
3Elfogadva30ms14868 KiB
4Elfogadva29ms15204 KiB
5Elfogadva28ms15164 KiB
6Elfogadva28ms15500 KiB
7Elfogadva28ms15560 KiB
subtask310/10
8Elfogadva35ms15928 KiB
9Elfogadva32ms16108 KiB
10Elfogadva35ms16384 KiB
11Elfogadva32ms16532 KiB
12Elfogadva32ms16592 KiB
subtask420/20
13Elfogadva96ms16948 KiB
14Elfogadva94ms16920 KiB
15Elfogadva100ms16980 KiB
16Elfogadva97ms16860 KiB
17Elfogadva100ms16860 KiB
18Elfogadva96ms17048 KiB
subtask529/29
19Elfogadva842ms20008 KiB
20Elfogadva876ms19912 KiB
21Elfogadva866ms19960 KiB
22Elfogadva884ms20008 KiB
23Elfogadva882ms19960 KiB
subtask631/31
24Elfogadva1.715s23008 KiB
25Elfogadva1.71s23032 KiB
26Elfogadva1.763s23032 KiB
27Elfogadva1.804s23032 KiB
28Elfogadva1.819s23056 KiB
29Elfogadva1.812s23440 KiB
30Elfogadva1.86s23288 KiB
31Elfogadva1.889s23168 KiB
32Elfogadva1.85s23168 KiB
33Elfogadva1.886s23468 KiB
34Elfogadva1.851s23296 KiB
35Elfogadva1.884s23320 KiB
36Elfogadva1.85s23600 KiB
37Elfogadva1.871s23352 KiB
38Elfogadva1.87s23384 KiB
39Elfogadva1.868s23356 KiB
40Elfogadva1.873s23404 KiB