# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
567654 | toastifishi | Split the sequence (APIO14_sequence) | C++14 | 13 ms | 4432 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 x first
#define y second
using namespace std;
using ll = long long;
const ll MAXN = 1e5 + 1;
const ll MAXK = 201;
struct line {
ll x, y, z;
};
struct hull {
int i;
vector <line> L, tt;
void init() {
i = 0;
L = tt;
}
ll f(ll ii, ll x) { return L[ii].x * x + L[ii].y; }
void push(line l) {
int sz = L.size() - 1;
if(sz >= 0 && l.x == L[sz].x) {
L[sz].z = l.z;
return;
}
while(sz > 0 && (L[sz].y - l.y) * (L[sz].x - L[sz - 1].x) < (L[sz - 1].y - L[sz].y) * (l.x - L[sz].x)) {
sz--;
L.pop_back();
}
L.push_back(l);
}
pair <ll, ll> query(ll pos) {
if(L.size() == 1) return {f(0, pos), L[0].z};
while(i + 1 < L.size() && f(i + 1, pos) > f(i, pos)) i++;
return {f(i, pos), L[i].z};
}
} CHT;
int n, k;
vector <int> res;
int track[MAXK][MAXN];
int arr[MAXN], prefix[MAXN];
ll dp[2][MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> arr[i];
prefix[i] = prefix[i - 1] + arr[i];
}
for(int i = 1; i <= k; i++) {
CHT.init();
CHT.push({0, 0, 0});
for(int j = 1; j <= n; j++) {
auto cur = CHT.query(prefix[j]);
dp[i & 1][j] = cur.x;
track[i][j] = cur.y;
CHT.push({prefix[j], dp[i - 1 & 1][j] - prefix[j] * prefix[j], j});
}
}
cout << dp[k & 1][n] << "\n";
int cur = n;
res.push_back(-1);
for(int i = k; i >= 1; i--) {
res.push_back(track[i][cur]);
cur = track[i][cur];
}
sort(res.begin(), res.end());
for(int i = 1; i <= k; i++) {
if(!res[i]) res[i] = 1;
if(res[i] <= res[i - 1]) res[i] = res[i - 1] + 1;
}
for(int i = 1; i <= k; i++) cout << res[i] << " ";
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... |