Submission #1066936

#TimeUsernameProblemLanguageResultExecution timeMemory
1066936parlimoos수열 (APIO14_sequence)C++14
0 / 100
12 ms5644 KiB
//Be Name KHODA
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define cl clear
#define bg begin
#define arr(x) array<ll , x>
#define endl '\n'

struct line{
    ll ar , sh;
};

int n , k;
ll ps[100000] , dp[100000][201];
int opt[100000][201];
vector<int> cnv;
vector<arr(2)> pts;
line ls[100000];

ll form(int i , int j , int t){
    return (ps[i] - ps[j]) * ps[j] + dp[j][t - 1];
}
bool isDel(line a , line b , line c){
    __int128_t d1 = (a.ar - b.ar) , d2 = (b.sh - a.sh);
    __int128_t d3 = (b.ar - c.ar) , d4 = (c.sh - b.sh);
    if(d2 < 0ll) d1 *= (-1ll) , d2 *= (-1ll);
    if(d4 < 0ll) d3 *= (-1ll) , d4 *= (-1ll);
    return ((d1 * d4) <= (d2 * d3));
}
ll getX(line a , line b){
    ll d1 = a.ar - b.ar , d2 = b.sh - a.sh;
    if(d2 < 0ll) d1 *= (-1ll) , d2 *= (-1ll);
    return ((d1 + d2 - 1) / d2);
}
void addLine(int e){
    if(cnv.empty()){
        cnv.pb(e) , pts.pb({0 , e});
        return;
    }
    int len = (int)cnv.size();
    while(len >= 2 and isDel(ls[e] , ls[cnv[len - 1]] , ls[cnv[len - 2]])){
        cnv.pp() , len--;
    }
    if(len == 1 and ls[e].sh == ls[cnv[len - 1]].sh){
        if(ls[e].ar >= ls[cnv[len - 1]].ar){
            cnv.pp();
            cnv.pb(e) , pts.pb({max(ps[e] + 1 , 0ll) , e});
        }
        return;
    }
    // if(e == 1) cout << getX(ls[e] , ls[cnv[len - 1]]) << "**\n";
    arr(2) d = {max(getX(ls[e] , ls[cnv[len - 1]]) , ps[e] + 1) , e};
    pts.pb(d) , cnv.pb(e);
}
void f(){
    for(int i = 0 ; i < n ; i++){
        ls[i].ar = dp[i][0] - (ps[i] * ps[i]);
        ls[i].sh = ps[i];
        addLine(i);
    }
    for(int t = 1 ; t <= k ; t++){
        int it = 0;
        for(int i = 0 ; i < n ; i++){
            while(it < (int)pts.size() - 1 and ps[i] >= pts[it + 1][0]) it++;
            int j = pts[it][1];
            if(j == i){
                cout << "%\n";
                exit(0);
            }    
            // if(t == 1 and i == 2) cout << pts[0][1] << "^^\n";
            dp[i][t] = form(i , j , t) , opt[i][t] = j;
        }
        cnv.cl() , pts.cl();
        for(int i = 0 ; i < n ; i++){
            ls[i].ar = dp[i][t] - (ps[i] * ps[i]);
            ls[i].sh = ps[i];
            addLine(i);
        }
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> k;
    for(int i = 0 ; i < n ; i++) cin >> ps[i];
    for(int i = 1 ; i < n ; i++) ps[i] += ps[i - 1];
    f();
    cout << dp[n - 1][k] << endl;
    vector<int> o;
    for(int v = n - 1 , i = k ; i >= 1 ; i--){
        o.pb(opt[v][i]);
        v = opt[v][i];
    }
    reverse(o.bg() , o.end());
    for(int el : o) cout << el + 1 << " ";
}
#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...