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;
const long long int INF=12345678912345678;
const int maxn=100010;
const int maxk=210;
int n, d;
int a[maxn];
long long int parc[maxn], parc_2[maxn];
long long int s_2[maxn];
long long int S_2(int i, int j){
return s_2[j]-s_2[i-1]-parc[i-1]*(parc[j]-parc[i-1]);
}
long long int dp[maxn][maxk];
int opt[maxn][maxk];
void resolve(int k, int ini, int fin, int opt_min, int opt_max){
int m=(ini+fin)/2;
dp[m][k]=INF;
for(int l=opt_min; l<=opt_max; l++){
if(dp[m][k]>dp[l][k-1]+S_2(l+1, m)){
dp[m][k]=dp[l][k-1]+S_2(l+1, m);
opt[m][k]=l;
}
}
if(ini!=fin){
resolve(k, ini, m, opt_min, opt[m][k]);
resolve(k, m+1, fin, opt[m][k], opt_max);
}
return;
}
int main(){
scanf("%d %d", &n, &d);
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
parc[i]=parc[i-1]+a[i];
parc_2[i]=parc_2[i-1]+a[i]*a[i];
s_2[i]=(parc[i]*parc[i]-parc_2[i])/2;
}
for(int i=1; i<=n; i++) dp[i][0]=s_2[i];
for(int k=1; k<=d; k++){
resolve(k, 1, n, 1, n);
}
printf("%lld\n", s_2[n]-dp[n][d]);
/* for(int i=1; i<=n; i++){
for(int k=1;k<=d; k++){
printf("%d ", dp[i][k]);
}
printf("\n");
}
*/
int at=n;
vector<int> aux;
for(int i=d; i>=1; i--){
aux.push_back(opt[at][i]);
at=opt[at][i];
}
for(int i=d-1; i>=0; i--) printf("%d ", aux[i]);
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:36:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &d);
~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:38:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
# | 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... |