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;
#define int long long
using ll = long long;
const int N = 1e5 + 10;
const int K = 200 + 10;
int n, k, a[N], dp[N][K], b[N][K];
struct line {
long long m, c, id;
long long eval(long long x) { return 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();
dp[i][l] = dq.back().eval(a[i]); b[i][l] = dq.back().id;
}
if(i > l - 1){
line cur = {a[i], -a[i]*a[i] + dp[i][l - 1], i};
while(dq.size() >= 2 && cur.intersectX(dq[0]) >= cur.intersectX(dq[1]))
dq.pop_front();
dq.push_front(cur);
}
}
}
cout << dp[n][k] << "\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... |