이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)
struct func {
int k; ll b;
func(): k(), b() {}
func(int k, ll b): k(k), b(b) {}
ll get(int x) { return 1LL * k * x + b; }
double inter(func o) {
return -(double)(b - o.b) / (k - o.k);
}
};
int n, k, a[100005], s[100005];
ll dp[100005], ndp[100005];
int pr[100005][205];
func f[100005];
int que[100005], ql, qr;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
rep(i, n) cin >> a[i], s[i + 1] = s[i] + a[i];
rep(j, k) {
ql = qr = 0;
rep(i, n) {
f[i] = func(s[i], dp[i] - 1LL * s[i] * s[i]);
while (qr - ql >= 2 && f[que[qr - 2]].inter(f[i]) <= f[que[qr - 2]].inter(f[que[qr - 1]])) -- qr;
que[qr ++] = i;
while (qr - ql >= 2 && f[que[ql]].get(s[i + 1]) <= f[que[ql + 1]].get(s[i + 1])) ++ ql;
ndp[i + 1] = f[que[ql]].get(s[i + 1]);
pr[i + 1][j + 1] = que[ql];
}
memcpy(dp, ndp, sizeof(dp));
}
cout << dp[n] << endl;
vector <int> ans;
int i = n, j = k;
while (j) i = pr[i][j], -- j, ans.pb(i);
reverse(ans.begin(), ans.end());
rep(i, ans.size()) cout << ans[i] << " ";
cout << endl;
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... |