Submission #200806

#TimeUsernameProblemLanguageResultExecution timeMemory
200806nvmdavaSplit the sequence (APIO14_sequence)C++17
100 / 100
714 ms100088 KiB
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define ff first
#define ss second
#define N 100001
#define K 202
#define MOD 1000000007
 
ll p[N];
int opt[N][K];
struct Line{
    ll y, g, x;
    int i;
    Line(ll _y, ll _g, int _i){
        i = _i;
        y = _y;
        g = _g;
        x = 0;
    }
};
 
struct Hull{
    deque<Line> line;
    int sz = 0;
 
    ll div(ll y, ll x){
        if(x == 0 || y <= 0)
            return 0;
        return y / x + (y % x != 0);
    }
 
    void addLine(ll y, ll g, int i){
        if(sz && line.back().g == g && line.back().y >= y){
            if(line.back().y == y)
                line.back().i = i;
            return;
        }
        Line tmp = Line(y, g, i);
        while(sz){
            tmp.x = div(line.back().y - tmp.y, tmp.g - line.back().g);
            if(tmp.x > line.back().x)
                break;
            line.pop_back();
            --sz;
        }
        ++sz;
        line.push_back(tmp);
    }
 
    pair<ll, int> query(ll x){
        if(!sz)
            return {0, 0};
        while(sz >= 2 && line[1].x <= x){
            line.pop_front();
            sz--;
        }
        return {line[0].y + line[0].g * x, line[0].i};
    }
} hull[K];
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int n, k;
    cin>>n>>k;
    ++k;
    for(int i = 1; i <= n; ++i){
        cin>>p[i];
        p[i] += p[i - 1];
    }
    for(int i = 1; i <= n; ++i){
        for(int j = k; j > 0; --j){
            auto a = hull[j - 1].query(p[i]);
            opt[i][j] = a.ss;
            hull[j].addLine(a.ff - p[i] * p[i], p[i], i);
        }
    }
    cout<<hull[k].line.back().y + p[n] * p[n]<<'\n';
    for(int i = k; i >= 2; i--){
        n = opt[n][i];
        cout<<n<<' ';
    }
}
#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...