Submission #109572

#TimeUsernameProblemLanguageResultExecution timeMemory
109572PeppaPigSplit the sequence (APIO14_sequence)C++14
60 / 100
2020 ms89260 KiB
#include <bits/stdc++.h>

#define long long long

using namespace std;

const int N = 1e5+5;

struct line {
    long m, b;
    int idx;
    line(long m, long b, int idx) : m(m), b(b), idx(idx) { }
    long f(long x) { return m * x + b; }
};

struct cht {
    int ptr = 0;
    vector<line> l;
    bool bad(line a, line b, line c) { return (c.b - a.b) * (a.m - b.m) <= (b.b - a.b) * (a.m - c.m); }
    void add(long m, long b, int idx) {
        line now(m, b, idx);
        while(l.size() > 1 && bad(l[l.size()-2], l[l.size()-1], now))
            l.pop_back();
        l.emplace_back(now);
    }
    line query(long x) {
        while(ptr + 1 < l.size() && l[ptr].f(x) < l[ptr+1].f(x)) ++ptr;
        return l[ptr];
    }
    void clear() { l.clear(), ptr = 0; }
} hull[2];

int n, k, pref[N], pv[N][205];
long dp[N];

int main() {
    scanf("%d %d", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%d", pref+i);
    for(int i = 1; i <= n; i++) pref[i] += pref[i-1];
    hull[0].add(0, 0, 0);
    for(int j = 1; j <= k+1; j++) {
        hull[j & 1].clear();
        for(int i = 1; i <= n; i++) {
            line now = hull[~j & 1].query(pref[i]);
            dp[i] = now.f(pref[i]), pv[i][j] = now.idx;
            hull[j & 1].add(pref[i], dp[i] - 1ll * pref[i] * pref[i], i);
        }
    }
    printf("%lld\n", dp[n]);
    vector<int> ans;
    for(int i = n, j = k+1; i; i = pv[i][j], --j) ans.emplace_back(pv[i][j]);
    while(!ans.back()) ans.pop_back();
    sort(ans.begin(), ans.end());
    ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
    for(int i : ans) printf("%d ", i);
    printf("\n");

    return 0;
}

Compilation message (stderr)

sequence.cpp: In member function 'line cht::query(long long int)':
sequence.cpp:27:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(ptr + 1 < l.size() && l[ptr].f(x) < l[ptr+1].f(x)) ++ptr;
               ~~~~~~~~^~~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:38:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 1; i <= n; i++) scanf("%d", pref+i);
                                 ~~~~~^~~~~~~~~~~~~~
#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...