이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <tuple>
#include <vector>
#include <queue>
using namespace std;
using ll = long long;
using line = tuple<ll, ll, int>;
const int N = 1e5;
const int K = 200;
int n, k;
ll a[N + 5], pfx_sum[N + 5];
pair<ll, int> dp[K + 5][N + 5];
line best[K + 1];
queue<line> q[K + 5];
ll ps(int i) {
return pfx_sum[i];
}
ll ss(int i) {
return pfx_sum[n] - pfx_sum[i - 1];
}
ll f(line l, ll x) {
auto [m, b, i] = l;
return m * x + b;
}
int id(line l) {
return get<2>(l);
}
pair<ll, vector<int>> retrieve() {
ll x = 0;
vector<int> v = {-1};
for (int i = 1; i <= n; i++) {
if (dp[k][i].first > x) {
x = dp[k][i].first;
v.back() = i;
}
}
for (int i = k; i >= 2; i--) {
v.push_back(dp[i][v.back()].second);
}
return {x, v};
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
a[0] = pfx_sum[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pfx_sum[i] = a[i] + pfx_sum[i - 1];
}
for (int s = 1; s <= k; s++) {
best[s] = make_tuple(0, -1, -1);
}
for (int i = 1; i <= n; i++) {
for (int s = k; s >= 1; s--) {
dp[s][i] = {-1, -1};
while (not q[s - 1].empty() and f(q[s - 1].front(), ss(i + 1)) > f(best[s - 1], ss(i + 1))) {
best[s - 1] = q[s - 1].front();
q[s - 1].pop();
}
if (s == 1) {
dp[s][i].first = ps(i) * ss(i + 1);
dp[s][i].second = -1;
q[s].push(make_tuple(-ps(i), dp[s][i].first, i));
} else if (id(best[s - 1]) != -1) {
dp[s][i].first = ps(i) * ss(i + 1) + f(best[s - 1], ss(i + 1));
dp[s][i].second = id(best[s - 1]);
q[s].push(make_tuple(-ps(i), dp[s][i].first, i));
}
}
}
pair<ll, vector<int>> ans = retrieve();
cout << ans.first << '\n';
for (int i : ans.second) {
cout << i << ' ';
}
cout << '\n';
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... |