# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
700266 | nguyentunglam | Split the sequence (APIO14_sequence) | C++17 | 879 ms | 81472 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 + 2;
long long f[N], g[N], s[N];
int used[210][N];
struct line {
long long m, c;
int trace;
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, 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[i] = dq.back().eval(s[i]) + s[i] * s[n] - s[i] * s[i];
used[j][i] = dq.back().trace;
line cur = {s[i], g[i] - s[i] * s[n], i};
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);
}
for(int i = 1; i <= n; i++) g[i] = f[i];
}
cout << g[n] << endl;
int cur = n, j = k;
for(int j = k; j >= 2; j--) {
cur = used[j][cur];
cout << cur << " ";
}
}
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... |