# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
43702 | baactree | Split the sequence (APIO14_sequence) | C++14 | 30 ms | 4708 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;
int n, k;
ll dp[2][100005];
int par[205][100005];
ll sum[100005];
int arr[100005];
double calc(pair<ll, ll> a, pair<ll, ll> b) {
return ((double)a.second - b.second) / (b.first - a.first);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
sum[i] = sum[i - 1] + arr[i];
}
for (int i = 1; i <= k; i++) {
deque<pair<ll, ll> > dq;
int idx = 1;
int cur = i & 1;
int pre = (i - 1) & 1;
for (int j = 2; j <= n; j++) {
ll a = sum[j - 1];
ll b = dp[pre][j - 1] - sum[j - 1] * sum[j - 1];
pair<ll, ll> now = make_pair(a, b);
while (dq.size() > 1 && calc(dq.back(), now) <= calc(dq[dq.size() - 2], now))
dq.pop_back();
dq.push_back(now);
while (dq.size() > 1 && calc(dq[0], dq[1]) <= sum[j]) {
dq.pop_front();
idx++;
}
dp[cur][j] = dq[0].first * sum[j] + dq[0].second;
par[i][j] = idx;
}
}
printf("%lld\n", dp[k&1][n]);
while (k) {
printf("%d ", par[k][n]);
n = par[k][n];
k--;
}
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... |