This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define INF 2000000000000000000ll
int n, kk;
int h, t;
long long sum[MAXN];
int arr[MAXN];
long long dp[MAXN][2];
int where[MAXN][201];
long long X(int i){
return sum[i];
}
long long Y(int i){
return -dp[i][0]+sum[i]*sum[i];
}
long double slope(int i, int j){
if(X(i) == X(j)) return Y(j) > Y(i) ? INF : -INF;
return ((long double)Y(j)-(long double)Y(i))/((long double)X(j)-(long double)X(i));
}
long long cost(int i, int j){
return dp[j][0]-sum[j]*sum[j]+sum[i]*sum[j];
}
int main(){
// freopen("a.in", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> kk;
for(int i = 1; i<=n; i++) cin >> arr[i];
for(int i = 1; i<=n; i++) sum[i] = sum[i-1]+arr[i];
for(int k = 1; k<=kk; k++){
h = 1, t = 0;
int hull[n+100];
hull[++t] = 0;
dp[0][1] = 0;
for(int i = 1; i<=n; i++){
while(h < t && slope(hull[h], hull[h+1]) <= (long double) sum[i]) ++h;
where[i][k] = hull[h];
dp[i][1] = cost(i, hull[h]);
while(h < t && slope(hull[t-1], hull[t]) >= slope(hull[t], i)) --t;
hull[++t] = i;
}
for(int i = 0; i<=n; i++) dp[i][0] = dp[i][1];
}
cout << dp[n][1] << endl;
int cur = n;
for(int k = kk; k>=1; --k){
cout << where[cur][k] << " ";
cur = where[cur][k];
}
}
# | 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... |