# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
197945 | songc | Split the sequence (APIO14_sequence) | C++14 | 344 ms | 131076 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;
typedef long long LL;
typedef pair<LL,LL> pii;
int N, K;
LL S[101010];
int P[101010][220];
struct Line{
LL m, k;
int id;
};
bool popchk(Line a, Line b, Line c){
return ((__int128)b.k-c.k)*(b.m-a.m) <= ((__int128)a.k-b.k)*(c.m-b.m);
}
struct ConvHull{
vector<Line> V;
int p;
void push(Line l){
if (!V.empty() && V.back().m == l.m) return;
while (V.size()>2 && popchk(V[V.size()-2], V.back(), l)){
V.pop_back();
if (V.size() <= p) p--;
}
V.push_back(l);
}
pii read(LL x){
while (p < V.size()-1 && V[p].m*x+V[p].k <= V[p+1].m*x+V[p+1].k) p++;
return pii(V[p].m*x+V[p].k, V[p].id);
}
} CHT[220];
int main(){
scanf("%d %d", &N, &K);
K++;
for (int i=1; i<=N; i++){
scanf("%lld", &S[i]);
S[i] += S[i-1];
}
CHT[0].push((Line){0, 0, 0});
for (int i=1; i<=N; i++){
for (int j=1; j<=min(i, K); j++){
LL ret;
tie(ret, P[i][j]) = CHT[j-1].read(S[i]);
if (i == N && j == K) printf("%lld\n", ret);
CHT[j].push((Line){S[i], ret-S[i]*S[i], i});
}
}
for (int i=K-1, p=P[N][K]; i; p=P[p][i], i--) printf("%d ", p);
printf("\n");
return 0;
}
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... |