Submission #856295

#TimeUsernameProblemLanguageResultExecution timeMemory
856295thisistotallymyusernameSplit the sequence (APIO14_sequence)C++17
71 / 100
59 ms44252 KiB
#include <iostream>
#include <cstring>

using namespace std;

typedef long long LL;
typedef long double LD;

const int N = 100010, M = 210;

int n, k;
short p[N][M];
int q[N], hh, tt;
LL dp1[N], dp2[N]; // rolling array
LL s[N];

LD y(int i) {
    return dp2[i] - s[i] * s[i];
}

LD slope(int i, int j) {
    if (s[i] == s[j]) {
        return -1;
    }
    return (1.0 * y(i) - y(j)) / ((LD)s[j] - s[i]);
}

int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> s[i];
        s[i] += s[i - 1];
    }
    for (int i = 1; i <= k; i++) {
        hh = tt = 0; q[hh] = 0;
        memcpy(dp2, dp1, sizeof dp2);
        for (int j = 1; j <= n; j++) {
            while (hh < tt && slope(q[hh], q[hh + 1]) <= s[j]) {
                hh++;
            }
            int t = q[hh];
            p[j][i] = t;
            dp1[j] = dp2[t] + s[t] * (s[j] - s[t]);
            while (hh < tt && slope(q[tt - 1], q[tt]) >= slope(q[tt], j)) {
                tt--;
            }
            q[++tt] = j;
        }
    }
    cout << dp1[n] << "\n";
    while (p[n][k]) {
        cout << p[n][k] << ' ';
        n = p[n][k--];
    }
    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...