이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int64_t INF = 1ll << 60;
const int MAX_N = 100005, MAX_K = 205;
vector<int64_t> dp, new_dp;
vector<int64_t> pref, pref_sq;
int path[MAX_N][MAX_K];
int64_t get_weight(int l, int r) {
int64_t sum = pref[r] - pref[l];
int64_t sum_sq = pref_sq[r] - pref_sq[l];
return sum * sum - sum_sq;
}
void divide(int ki, int l, int r, int optl, int optr) {
if (l >= r) {
return;
}
int mid = (l + r) / 2;
int opt;
for (int i = optl; i < min(mid, optr); i++) {
int64_t value = dp[i] + get_weight(i, mid);
if (new_dp[mid] > value) {
new_dp[mid] = value;
opt = i;
}
}
path[mid][ki] = opt;
divide(ki, l, mid, optl, opt + 1);
divide(ki, mid + 1, r, opt, optr);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, K;
cin >> N >> K;
vector<int> A(N);
for (int &v : A) {
cin >> v;
}
K++;
pref = pref_sq = vector<int64_t>(N + 1);
for (int i = 0; i < N; i++) {
pref[i + 1] = pref[i] + A[i];
pref_sq[i + 1] = pref_sq[i] + A[i] * A[i];
}
memset(path, -1, sizeof path);
dp = vector<int64_t>(N + 1, INF);
dp[0] = 0;
for (int i = 0; i < K; i++) {
new_dp = vector<int64_t>(N + 1, INF);
divide(i + 1, 0, N + 1, 0, N + 1);
dp = new_dp;
}
cout << (get_weight(0, N) - dp[N]) / 2 << "\n";
int pos = N, ki = K;
vector<int> ans;
while (pos > 0) {
ans.push_back(pos = path[pos][ki--]);
}
ans.pop_back();
reverse(ans.begin(), ans.end());
for (int i = 0; i < K - 1; i++) {
cout << ans[i] << " \n"[i + 1 == K - 1];
}
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'void divide(int, int, int, int, int)':
sequence.cpp:30:9: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
30 | divide(ki, l, mid, optl, opt + 1);
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |