# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
501264 | Ziel | Split the sequence (APIO14_sequence) | C++17 | 78 ms | 131076 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.
/**
* LES GREATEABLES BRO TEAM
**/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");
const ll INF = 10000000000000;
const int N = 100012, K = 212;
#define int ll
ll a[N], pref[N], dp[K][N], last[K][N];
struct line {
ll k, b, id;
ll eval(ll x) {
return x * k + b;
}
};
double crossX(line a, line b) {
double num = a.b - b.b;
double den = b.k - a.k;
if (den == 0) {
return -INF;
}
return (double)(num / den);
}
void solve() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
for (int j = 0; j <= k; j++)
dp[j][i] = -INF;
}
deque<line> dq;
for (int i = 1; i < n; i++) {
dp[1][i] = pref[i] * pref[n] - pref[i] * pref[i];
last[1][i] = n + 1;
}
for (int j = 2; j <= k; j++) {
dq.clear();
for (int i = n - 1; i >= 1; i--) {
while (sz(dq) >= 2 && dq[sz(dq) - 2].eval(pref[i]) > dq[sz(dq) - 1].eval(pref[i]))
dq.pop_back();
if (sz(dq)) {
dp[j][i] = dq.back().eval(pref[i]) - pref[i] * pref[i];
last[j][i] = dq.back().id;
}
if (dp[j - 1][i] == -INF)
continue;
line cur = {pref[i], dp[j - 1][i], i};
while (sz(dq) >= 2 && crossX(cur, dq[0]) >= crossX(dq[0], dq[1]))
dq.pop_front();
dq.push_front(cur);
}
}
ll ans = dp[k][1], cur = 1;
for (int i = 1; i < n; i++) {
if (dp[k][i] > ans)
ans = dp[k][i], cur = i;
}
cout << ans << '\n';
for (int i = k; i >= 1; i--) {
if (cur > n || cur < 1)
break;
cout << cur << ' ';
cur = last[i][cur];
}
}
signed main() {
setIO();
int tt = 1;
if (FLAG) {
cin >> tt;
}
while (tt--) {
solve();
}
return 0;
}
void setIO(const string &f) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen((f + ".in").c_str(), "r")) {
freopen((f + ".in").c_str(), "r", stdin);
freopen((f + ".out").c_str(), "w", stdout);
}
}
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... |