# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
700254 | nguyentunglam | Split the sequence (APIO14_sequence) | C++17 | 17 ms | 4172 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>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 10;
long long f[210][N];
long long s[N];
struct line {
long long m, c;
long long eval(long long x) { return m * x + c; }
long long calcx (line l) { return (c - l.c) / (l.m - m); }
};
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int n, k; cin >> n >> k;
++k;
for(int i = 1; i <= n; i++) {
cin >> s[i];
s[i] += s[i - 1];
}
for(int j = 1; j <= k; j++) {
deque<line> dq;
dq.push_back({0, 0});
for(int i = 1; i <= n; i++) {
while (dq.size() >= 2 && dq.back().eval(s[i]) <= dq[dq.size() - 2].eval(s[i])) dq.pop_back();
f[j][i] = dq.back().eval(s[i]) + s[i] * s[n] - s[i] * s[i];
line cur = {s[i], f[j - 1][i] - s[i] * s[n]};
if (!dq.empty() && dq.front().m == cur.m) {
if (cur.c > dq.front().c) dq.pop_front();
else continue;
}
while (dq.size() >= 2 && cur.calcx(dq[0]) >= dq[0].calcx(dq[1])) dq.pop_front();
dq.push_front(cur);
}
}
cout << f[k][n] << endl;
int cur = n;
vector<int> ans;
for(int j = k; j >= 2; j--) {
for(int i = cur - 1; i >= 1; i--) if (f[j - 1][i] + (s[cur] - s[i]) * (s[n] - s[cur]) == f[j][cur]) {
cur = i;
ans.push_back(cur);
break;
}
}
reverse(ans.begin(), ans.end());
for(int &j : ans) cout << j << " ";
}
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... |