This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define wiwihorz
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)
using namespace std;
#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#ifdef wiwihorz
#define print(a...) cerr << "Line " << __LINE__ <<": ", kout("[" + string(#a) + "] = ", a)
void kout() {cerr << endl;}
void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...); }
#else
#define print(...) 0
#define vprint(...) 0
#endif
#define a first
#define b second
namespace solver {
int n, k;
vector<int> dp[2], pre, a, pos;
vector<vector<int>> rec;
int get_val(pii p, int x) {
return p.a * x + p.b;
}
bool check(pii i, pii j, pii k) {
return (i.a - j.a) * (k.b - i.b) <
(j.b - i.b) * (i.a - k.a);
}
void init_(int _n, int _k) {
n = _n, k = _k;
dp[0].assign(n + 1, 0);
dp[1].assign(n + 1, 0);
pre.assign(n + 1, 0);
a.assign(n + 1, 0);
rec.assign(k + 2, vector<int>(n + 1, 0));
}
void solve() {
vector<pii> q(n + 1, {0, 0});
pos.assign(n + 1, 0);
rep(i, 1, n) pre[i] = pre[i - 1] + a[i];
rep(l, 1, k + 1) {
int id = l & 1, L = 1, R = 1;
q[1] = {-pre[l - 1], dp[!id][l - 1]};
pos[1] = l - 1;
rep(i, l, n) {
int suf = pre[n] - pre[i];
while(L < R && get_val(q[L], suf) <=
get_val(q[L + 1], suf)) L ++;
dp[id][i] = get_val(q[L], suf) + pre[i] * suf;
rec[l][i] = pos[L];
pii cur = {-pre[i], dp[!id][i]};
while(R > L && !check(q[R - 1], q[R], cur)) R--;
q[++R] = cur, pos[R] = i;
}
}
cout << dp[(k + 1) & 1][n] << "\n";
vector<int> ans(k + 1, 0);
int cur = n;
rrep(i, 1, k) {
ans[i] = rec[i + 1][cur];
cur = rec[i + 1][cur];
}
rep(i, 1, k) cout << ans[i] << " \n"[i == k];
}
};
using namespace solver;
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, k;
cin >> n >> k;
init_(n, k);
rep(i, 1, n) cin >> a[i];
solve();
return 0;
}
Compilation message (stderr)
sequence.cpp:3: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
3 | #pragma loop-opt(on)
|
sequence.cpp:25:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
25 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
| ^~~~
sequence.cpp:25:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
25 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
| ^~~~
# | 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... |