Submission #838510

#TimeUsernameProblemLanguageResultExecution timeMemory
838510tch1cherinSplit the sequence (APIO14_sequence)C++17
100 / 100
1309 ms85352 KiB
#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 = optl; 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]; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...