이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
void main_program();
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
main_program();
}
using ll = long long;
const ll inf = 1e18;
struct Line {
mutable ll k, m, p;
int idx;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m, int idx) {
auto z = insert({k, m, 0, idx}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
pair<ll, int> query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return {l.k * x + l.m, l.idx};
}
};
int n, k;
vector<ll> v, pfx;
void main_program(){
cin >> n >> k;
v.resize(n + 1);
for (int i = 1; i <= n; i++) cin >> v[i];
pfx.resize(n + 1);
for (int i = 1; i <= n; i++) pfx[i] = pfx[i-1] + v[i];
vector<ll> dp(n + 1, -inf);
vector<vector<int>> trace(k + 2, vector<int>(n + 1));
dp[0] = 0;
for (int j = 1; j <= k + 1; j++){
LineContainer cht;
vector<ll> newdp(n + 1, -inf);
for (int i = 1; i <= n; i++){
// Add line (pfx[i-1] * S + dp[i-1] - pfx[i-1]^2)
cht.add(pfx[i-1], dp[i-1] - pfx[i-1] * pfx[i-1], i - 1);
// Find answer
tie(newdp[i], trace[j][i]) = cht.query(pfx[i]);
}
swap(dp, newdp);
}
cout << dp[n] << "\n";
vector<int> splits;
int crr = n;
for (int j = k + 1; j > 1; j--){
crr = trace[j][crr];
splits.push_back(crr);
}
reverse(splits.begin(), splits.end());
for (auto elem: splits) cout << elem << " ";
cout << "\n";
}
# | 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... |