# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
43705 | baactree | Split the sequence (APIO14_sequence) | C++14 | 820 ms | 84500 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];
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);
set<int> s;
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
sum[i] = sum[i - 1] + arr[i];
if (!arr[i])s.insert(i);
}
for (int i = 1; i <= k; i++) {
deque<int> dq;
int cur = i & 1;
int pre = (i - 1) & 1;
for (int j = 1; j <= n; j++) {
if (!arr[j]) {
dp[cur][j] = dp[cur][j - 1];
par[i][j] = par[i][j - 1];
continue;
}
while (dq.size() > 1 && calc(dd[dq[0]], dd[dq[1]]) <= sum[j])
dq.pop_front();
if (!dq.empty()) {
dp[cur][j] = dd[dq[0]].first * sum[j] + dd[dq[0]].second;
par[i][j] = dq[0];
}
ll a = sum[j];
ll b = dp[pre][j] - sum[j] * sum[j];
dd[j] = make_pair(a, b);
while (dq.size() > 1 && calc(dd[dq.back()], dd[j]) <= calc(dd[dq[dq.size() - 2]], dd[j]))
dq.pop_back();
dq.push_back(j);
}
}
printf("%lld\n", dp[k&1][n]);
vector<int> ans;
while (k) {
ans.push_back(par[k][n]);
n = par[k][n];
k--;
}
for (int i = 0; i < ans.size(); i++) {
s.erase(ans[i]);
if (!ans[i]) {
printf("%d ", *s.begin());
s.erase(s.begin());
}
else
printf("%d ", ans[i]);
}
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... |