# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
43708 | baactree | Split the sequence (APIO14_sequence) | C++14 | 987 ms | 83524 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];
pair<ll, ll> dd[100005];
long double calc(pair<ll, ll> a, pair<ll, ll> b) {
return ((long double)a.second - b.second) / (b.first - a.first);
}
void push(int i, int j,deque<int> &dq) {
ll a = sum[j];
ll b = dp[(i-1)&1][j] - sum[j] * sum[j];
dd[j] = make_pair(a, b);
while (dq.size() > 1 && (dd[dq.back()].second-dd[j].second)*(-dd[dq[dq.size() - 2]].first + dd[j].first) <= (dd[dq[dq.size() - 2]].second-dd[j].second)*(-dd[dq.back()].first + dd[j].first))
dq.pop_back();
dq.push_back(j);
}
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<int> dq;
int cur = i & 1;
int pre = (i - 1) & 1;
push(i,i,dq);
for (int j = i + 1; j <= n; j++) {
while (dq.size() > 1 && (dd[dq[0]].second - dd[dq[1]].second) <= sum[j]*(dd[dq[1]].first-dd[dq[0]].first))
dq.pop_front();
dp[cur][j] = dd[dq[0]].first * sum[j] + dd[dq[0]].second;
par[i][j] = dq[0];
push(i,j,dq);
}
}
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... |