This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10;
const int K = 200 + 10;
int n, k, b[N][K];
ll dp1[N], dp2[N], a[N];
struct line {
ll m, c;int id;
ll eval(ll x) { return (ll)(m * x) + c; }
long double intersectX(line l) { return (long double) (c - l.c) / (l.m - m); }
};
signed main(){
iostream::sync_with_stdio(false);cin.tie(nullptr);
cin >> n >> k;
for(int i = 1;i<=n;i++)cin >> a[i];
for(int i = 1;i<=n;i++)a[i] += a[i - 1];
for(int l = 1;l<=k;l++){
deque<line> dq;dq.push_front({0, 0, 0});
for(int i = 1;i<=n;i++){
if(i > l){
while(dq.size() >= 2 && dq.back().eval(a[i]) <= dq[dq.size() -2].eval(a[i]))dq.pop_back();
dp2[i] = dq.back().eval(a[i]); b[i][l] = dq.back().id;
}
if(i > l - 1){
line cur = {a[i], -a[i]*a[i] + dp1[i], i};
while(dq.size() >= 2 && cur.intersectX(dq[0]) <= cur.intersectX(dq[1]))
dq.pop_front();
dq.push_front(cur);
}
}
for(int i = 0;i<=n;i++)dp1[i] = dp2[i];
}
cout << dp1[n] << "\n";
vector<int> v = {n};
for(int l = k;l>=1;l--){
v.push_back(b[v.back()][l]);
}
reverse(v.begin(), v.end());v.pop_back();reverse(v.begin(), v.end());
for(int e : v)cout << e << " ";
cout << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |