# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
426250 | zoooma13 | Split the sequence (APIO14_sequence) | C++14 | 22 ms | 4084 KiB |
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; int idx;
int64_t eval(int64_t x) { return m*x + c; }
};
struct hull : deque <line> {
void add(int64_t m ,int64_t c ,int idx){
line l = {m ,c ,idx};
while(size() >= 2 && 1.0*(c - back().c)*(back().m - at(size()-2).m) <= 1.0*(back().m - m)*(at(size()-2).c - back().c))
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)
# | 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... |