This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
struct Line
{
ll m, c;
int idx;
Line() : m(0LL), c(0LL), idx(0) {}
Line(ll _m, ll _c, int _idx) : m(_m), c(_c), idx(_idx) {}
ll operator()(const ll &x) { return m * x + c; }
friend ld isect(const Line &a, const Line &b)
{
return (ld)(b.c - a.c) / (ld)(a.m - b.m);
}
};
int N, K, a[100001];
ll pref[100001] = {0};
ll dp[2][100001] = {0};
int last_split[202][100001];
Line cht[100001];
int fr = 0, bk = -1;
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> N >> K;
for (int i = 1; i <= N; i++)
{
cin >> a[i];
pref[i] = pref[i-1] + a[i];
}
for (int i = 0; i <= N; i++)
dp[0][i] = pref[i] * (pref[N] - pref[i]);
for (int k = 2; k <= K + 1; k++)
{
fr = 0, bk = -1;
for (int i = k; i <= N; i++)
{
Line newline(pref[i-1], dp[0][i-1] - pref[i-1] * pref[N], i-1);
while (fr < bk && isect(cht[bk], newline) <= isect(cht[bk-1], newline))
bk--;
cht[++bk] = newline;
while (fr < bk && cht[fr](pref[i]) <= cht[fr+1](pref[i]))
fr++;
dp[1][i] = cht[fr](pref[i]) + pref[i] * (pref[N] - pref[i]);
last_split[k][i] = cht[fr].idx;
}
swap(dp[0], dp[1]);
}
cout << dp[0][N] << '\n';
int i = N;
do
{
i = last_split[K+1][i];
cout << i << ' ';
}
while (--K);
cout << '\n';
return 0;
}
# | 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... |