# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
557403 | Ai7081 | Split the sequence (APIO14_sequence) | C++17 | 210 ms | 131072 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;
#define ll long long
#define pii pair<ll, ll>
const int N = 100005;
const int K = 205;
struct Line{
ll m, c, idx;
ll operator() (ll x) {return m*x + c;}
};
ll n, k, a, dp[K][N], sum[N], rev[K][N], ptr;
vector<Line> v;
bool check(Line x, Line y, Line z) {
return (z.c-y.c)*(x.m-y.m) < (y.c-x.c)*(y.m-z.m);
}
void add(Line L) {
while (v.size()>=2 && check(v[v.size()-2], v.back(), L)) v.pop_back();
while (!v.empty() && v.back().m == L.m && v.back().c <= L.c) v.pop_back();
v.push_back(L);
}
pii query(ll x) {
ptr = min(ptr, (ll)v.size()-1);
while (ptr != v.size()-1 && v[ptr](x) <= v[ptr+1](x)) ptr++;
return {v[ptr](x), v[ptr].idx};
}
int main() {
scanf(" %lld %lld", &n, &k);
k++;
for (int i=1; i<=n; i++) scanf(" %lld", &a), sum[i] = sum[i-1] + a;
for (int i=2; i<=k; i++) {
ptr = 0;
v.clear();
for (int j=i; j<=n; j++) {
add({sum[j-1], dp[i-1][j-1] - sum[j-1]*sum[j-1], j-1});
pii res = query(sum[j]);
dp[i][j] = res.first;
rev[i][j] = res.second;
}
}
printf("%lld\n", dp[k][n]);
vector<ll> out;
while (rev[k][n]) {
out.push_back(rev[k][n]);
n = rev[k][n];
k--;
}
for (int i=out.size()-1; i>=0; i--) printf("%lld ", out[i]);
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
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... |