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;
#ifdef LOCAL
#include <debug.h>
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
// mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
// ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
Tran The Bao
CTL - Da Lat
Cay ngay cay dem nhung deo duoc cong nhan
*/
struct Line {
ll a, b;
int id;
Line() {}
Line(ll a, ll b, int id) : a(a), b(b), id(id) {}
};
const ldb eps = 1e-7;
const ll oo = 2e18 + 5;
const int N = 1e5 + 1;
const int K = 202;
int n, k, s, f[N], pre[K][N];
ll d[N], d1[N];
ll cal(int f, ll a, ll b) { return 1LL * f * s - 1LL * f * f + 1LL * a * f + b; }
ldb it(Line a, Line b) {
if (a.a == b.a) return -oo;
return (ldb)(b.b - a.b) / (ldb)(a.a - b.a);
}
void trace(int i, int j) {
int prev = pre[i][j];
if (!prev) return;
cout << prev << ' ';
trace(i - 1, prev);
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n >> k;
FOR(i, 1, n) cin >> f[i];
FOR(i, 1, n) f[i] += f[i - 1];
s = f[n];
FOR(i, 1, n) d1[i] = -oo;
vector<Line> cv;
FOR(i, 1, k + 1) {
cv.pb(Line(0, d1[0], 0));
int l = 0;
d[0] = -oo;
FOR(j, 1, n) {
l = min(l, sz(cv) - 1);
WHILE(l < sz(cv) - 1 && (d1[cv[l].id] < 0 || cal(f[j], cv[l].a, cv[l].b) <= cal(f[j], cv[l + 1].a, cv[l + 1].b))) ++l;
if (d1[cv[l].id] >= 0) {
d[j] = cal(f[j], cv[l].a, cv[l].b);
pre[i][j] = cv[l].id;
}
else d[j] = -oo;
if (d1[j] < 0) continue;
Line cur(2 * f[j], d1[j] - 1LL * f[j] * s - 1LL * f[j] * f[j], j);
WHILE(sz(cv) > 1) {
Line tmp = cv[sz(cv) - 1], tmp1 = cv[sz(cv) - 2];
ldb x1 = it(tmp, tmp1), x2 = it(tmp, cur);
if (x2 <= x1 || x2 - x1 <= eps) {
cv._pb();
continue;
}
if (x2 == -oo) cv._pb();
break;
}
cv.pb(cur);
}
FOR(j, 0, n) d1[j] = d[j];
cv.clear();
}
cout << d[n] / 2 << '\n';
trace(k + 1, n);
return 0;
}
/*
7 3
4 1 3 4 0 2 3
5 1
0 9 6 3 0
*/
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
sequence.cpp:64:5: note: in expansion of macro 'FOR'
64 | FOR(i, 1, n) cin >> f[i];
| ^~~
sequence.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
sequence.cpp:65:5: note: in expansion of macro 'FOR'
65 | FOR(i, 1, n) f[i] += f[i - 1];
| ^~~
sequence.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
sequence.cpp:67:5: note: in expansion of macro 'FOR'
67 | FOR(i, 1, n) d1[i] = -oo;
| ^~~
sequence.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
sequence.cpp:69:5: note: in expansion of macro 'FOR'
69 | FOR(i, 1, k + 1) {
| ^~~
sequence.cpp:24:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
sequence.cpp:73:9: note: in expansion of macro 'FOR'
73 | FOR(j, 1, n) {
| ^~~
sequence.cpp:24:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
sequence.cpp:95:9: note: in expansion of macro 'FOR'
95 | FOR(j, 0, n) d1[j] = d[j];
| ^~~
# | 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... |