Submission #365137

#TimeUsernameProblemLanguageResultExecution timeMemory
365137salehSplit the sequence (APIO14_sequence)C++17
0 / 100
81 ms131076 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 100 * 1000 + 2, MAXK = 200 + 2; int n, k, chi; int par[MAXK][MAXN]; long long dp[MAXK][MAXN], ps[MAXN]; void cal(int l = chi, int r = n + 1, int ll = chi, int rr = n) { int mid = ((l + r) >> 1), md = -1; for (int i = ll; i < mid && i < r; i++) { if (dp[chi - 1][i] + ps[i] * (ps[mid] - ps[i]) > dp[chi][mid]) { dp[chi][mid] = dp[chi - 1][i] + ps[i] * (ps[mid] - ps[i]); md = i; } } par[chi][mid] = md; if (l < mid) cal(l, mid, ll, md); if (mid + 1 < r) cal(mid + 1, r, md, rr); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n >> k; int a; for (int i = 0; i < n; i++) { cin >> a; ps[i + 1] = ps[i] + a; } memset(dp, -1, sizeof dp); for (int i = 0; i < n; i++) dp[0][i] = 0; for (int i = 1; i <= k; i++) { chi = i; cal(); } cout << dp[k][n] << '\n'; while (k > 0) { cout << par[k][n] << ' '; n = par[k][n]; k--; } return 0; }
#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...