이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <string>
#include <numeric>
#include <cmath>
#include <cassert>
#include <array>
#include <numeric>
using namespace std;
using ll = long long;
const ll INF = 1e18;
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, k;
cin >> n >> k;
++k;
vector<ll> a(n);
for (ll &i : a) cin >> i;
array<vector<ll>, 2> dp;
dp[0] = dp[1] = vector<ll>(n + 1, INF);
dp[0][0] = 0;
ll res = accumulate(a.begin(), a.end(), 0ll);
res = res * res;
partial_sum(a.begin(), a.end(), a.begin());
a.insert(a.begin(), 0);
vector<vector<int>> parent(n + 1, vector<int>(k, -1));
auto dac = [&dp, &a, &parent](auto self, int l, int r, int l2, int r2, int level)
{
if (l > r) return;
int m = (l + r) >> 1, ind = l2;
for (int i = l2; i <= min(r2, m - 1); ++i)
{
ll cur = dp[0][i] + (a[m] - a[i]) * (a[m] - a[i]);
if (cur < dp[1][m])
{
dp[1][m] = cur;
ind = i;
}
}
parent[m][level] = ind;
self(self, l, m - 1, l2, ind, level);
self(self, m + 1, r, ind, r, level);
};
for (int i = 0; i < k; ++i)
{
dac(dac, i + 1, n, 0, n - 1, i);
//i -> i + 1
dp[0] = dp[1];
dp[1] = vector<ll>(n + 1, INF);
}
vector<int> path(k - 1);
for (int i = k - 1, v = n; i > 0; v = parent[v][i], --i)
{
path[k - i - 1] = parent[v][i];
//v = parent[v][i];
}
cout << (res - dp[0].back()) / 2 << '\n';
reverse(path.begin(), path.end());
for (int i : path) cout << i << ' ';
return 0;
}
/*
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... |