이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ld long double
#define ll long long
mt19937 rnd(51);
const ll INF = 1e18, K = 210;
struct line {
ll k, b;
int i;
};
vector<line> q, qq;
ld intersection(line a, line b) {
return (ld)(b.b - a.b) / (a.k - b.k);
}
void add(int i, line a) {
while (qq.size() > 1) {
int sz = qq.size();
if (qq.back().k == a.k) {
if (a.b > qq.back().b) {
qq.pop_back();
} else {
return;
}
}
if (intersection(qq[sz - 2], a) < intersection(qq[sz - 2], qq[sz - 1])) {
qq.pop_back();
} else {
break;
}
}
if (qq.size() == 1 && qq.back().k == a.k) {
if (a.b > qq.back().b) {
qq.pop_back();
} else {
return;
}
}
qq.pb(a);
}
pair<ll, int> get(int i, ll x) {
int l = -1, r = (int)(q.size()) - 1;
if (q.size() == 0) return {-INF, -1};
while (r - l > 1) {
int mid = (r + l) / 2;
if (intersection(q[mid], q[mid + 1]) <= x) {
l = mid;
} else {
r = mid;
}
}
return {x * q[r].k + q[r].b, q[r].i};
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LCOAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<ll> a(n), pr(n);
vector<vector<int>> par(n + 1, vector<int>(k + 1));
for (int i = 0; i < n; i++) {
cin >> a[i];
}
reverse(a.begin(), a.end());
for (int i = 0; i < n; i++) {
pr[i] = a[i];
if (i > 0) {
pr[i] += pr[i - 1];
}
}
if (n == k + 1) {
ll ans = 0;
for (int i = 1; i < n; i++) {
ans += a[i] * pr[i - 1];
}
cout << ans << endl;
for (int i = 1; i <= k; i++) {
cout << i << ' ';
}
cout << endl;
return 0;
}
ll ans = -1;
for (int i = 0; i < n; i++) {
add(0, {pr[i], 0 - pr[i] * pr[i], i});
}
for (int f = 1; f <= k; ++f) {
q = qq;
qq.clear();
for (int i = 0; i < n; ++i) {
pair<ll, int> p = get(f - 1, pr[i]);
if (i == n - 1 && f == k) {
ans = p.first;
}
par[i][f] = p.second;
add(f, {pr[i], p.first - pr[i] * pr[i], i});
}
}
cout << ans << endl;
vector<int> res;
int i = n - 1, j = k, sum = 0;
while (j > 0) {
res.pb(i - par[i][j]);
i = par[i][j];
j--;
sum += res.back();
}
int now = 0;
for (auto to : res) {
cout << (now += to) << ' ';
}
cout << endl;
return 0;
}
# | 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... |