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>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long double ld;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 100100;
const ll INF = 1e16;
/*struct line {
ll k, b;
int id;
line (ll _k, ll _b, int _id) : k (_k), b (_b), id (_id) {}
ll operator() (ll x) {
return k * x + b;
}
};*/
/*ld intersection (line p, line q) {
ld dy = ((ld)(q.b - p.b));
ld dx = ((ld)(p.k - q.k));
return dy / dx;
}*/
ld intersection (pair<pair<ll, ll>, int> p, pair<pair<ll, ll>, int> q) {
ld dy = ((ld)(q.f.s - p.f.s));
ld dx = ((ld)(p.f.f - q.f.f));
return dy / dx;
}
//ll dp[210][MAXN];
int pr[210][MAXN];
int n, k;
ll a[MAXN], pref[MAXN];
deque<pair<pair<ll, ll>, int>> dq[210];
//int lstId[210];
void ins (int g, int id, ll dp) {
ll k = pref[id];
ll b = dp - pref[id] * pref[id];
// line newL (k, b, id);
pair<pair<ll, ll>, int> newL = {{k, b}, id};
while (dq[g].size() > 0 && dq[g][dq[g].size()-1].f.f == newL.f.f) {
dq[g].pop_back();
}
while (dq[g].size() > 1 &&
intersection(dq[g][dq[g].size()-2], newL) < intersection(dq[g][dq[g].size()-2], dq[g][dq[g].size()-1])) {
dq[g].pop_back();
}
dq[g].push_back(newL);
}
pair<int, ll> getMaxId (int g, ll x) {
while (dq[g].size() > 1 && x >= intersection(dq[g][0], dq[g][1])) {
dq[g].pop_front();
}
return {dq[g][0].s, dq[g][0].f.f * x + dq[g][0].f.s};
}
int main()
{
FAST_IO;
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
FOR(i, 1, n) pref[i] = pref[i-1] + a[i];
ins(0, 0, 0);
FOR(i, 1, n) for (int g = k+1; g > 0; g--) {
if (i < g) continue;
ll x = pref[i];
auto p = getMaxId(g-1, x);
int j = p.f;
ll dp = p.s;
pr[g][i] = j;
//lstDp = dp;
if (i == n && g == k+1)
cout << dp << "\n";
//cout << " i = " << i << " g = " << g << " dp = " << dp << endl;
ins(g, i, dp);
}
//cout << lstDp << "\n";
int g = k+1, i = n;
while (g > 0) {
i = pr[g][i];
g--;
if (g > 0) cout << i << " ";
}
cout << "\n";
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
# | 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... |