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;
int64_t sqr(int x){
return 1LL*x*x;
}
struct line{
int64_t m ,c ,idx;
int64_t eval(int64_t x) { return m*x + c; }
long double ix(line&l) { return (long double)(l.c - c)/(m - l.m); }
};
struct hull : deque <line> {
void add(int64_t m ,int64_t c ,int idx){
line l = {m ,c ,idx};
while(size() >= 2 && l.ix(back()) <= back().ix(at(size()-2)))
pop_back();
push_back(l);
}
pair <int64_t ,int> eval(int64_t x){
while(size() >= 2 && at(0).eval(x) <= at(1).eval(x))
pop_front();
return {at(0).eval(x) , at(0).idx};
}
};
int main()
{
int n ,m;
scanf("%d%d",&n,&m);
vector <int> a(n) ,s{0};
for(int&i : a){
scanf("%d",&i);
s.push_back(s.back() + i);
}
vector <int64_t> dp(n+1) ,last_dp(n+1);
vector <vector <int>> trace(1 ,vector <int> (n+1 ,0));
for(int i = 0; i <= n; i++)
dp[i] = sqr(s[i]);
for(int k = 1; k <= m; k++){
last_dp = dp;
trace.push_back(vector <int> (n+1 ,0));
auto&from = trace.back();
hull h;
h.add(s[k] ,-sqr(s[k])-last_dp[k] ,k);
for(int i = k+1; i <= n; i++){
auto r = h.eval(2*s[i]);
dp[i] = sqr(s[i]) - r.first;
from[i] = r.second;
h.add(s[i] ,-sqr(s[i])-last_dp[i] ,i);
}
}
printf("%lld\n",(sqr(s.back()) - dp.back()) / 2);
for(int l = m ,i = n; l > 0; l--){
i = trace[l][i];
printf("%d ",i);
}
printf("\n");
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:55:16: warning: format '%lld' expects argument of type 'long long int', but argument 2 has type 'int64_t' {aka 'long int'} [-Wformat=]
55 | printf("%lld\n",(sqr(s.back()) - dp.back()) / 2);
| ~~~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| long long int int64_t {aka long int}
| %ld
sequence.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
sequence.cpp:33:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | scanf("%d",&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... |