Submission #729544

#TimeUsernameProblemLanguageResultExecution timeMemory
729544danikoynovSplit the sequence (APIO14_sequence)C++14
100 / 100
1264 ms82544 KiB
#include<bits/stdc++.h> #define endl '\n' using namespace std; typedef long long ll; void speed() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const int maxn = 1e5 + 10, maxk = 210; const ll inf = 1e18; int n, k; ll a[maxn], pref[maxn], dp[2][maxn]; int bef[maxk][maxn]; void input() { cin >> n >> k; k ++; for (int i = 1; i <= n; i ++) cin >> a[i]; } void calculate_prefix() { for (int i = 1; i <= n; i ++) pref[i] = pref[i - 1] + a[i]; } ll get_range(int l, int r) { return pref[r] - pref[l - 1]; } ll cost(int l, int r) { return pref[r] * pref[n] - pref[r] * pref[r] - pref[l - 1] * pref[n] + pref[l - 1] * pref[r]; } void solve_layer(int layer, int left, int right, int optl, int optr) { ///cout << layer << " " << left << " " << right << endl; if (left > right) return; int opt = -1, mid = (left + right) / 2; ll best = -inf; for (int i = optl; i <= min(mid, optr); i ++) { ll sum = cost(i, mid) + dp[(layer + 1) % 2][i - 1]; if (sum > best) { best = sum; opt = i; } } if (opt == -1) { opt = 1; } bef[layer][mid] = opt - 1; dp[layer % 2][mid] = best; solve_layer(layer, left, mid - 1, optl, opt); solve_layer(layer, mid + 1, right, opt, optr); } void calculate_states() { for (int j = 0; j <= 1; j ++) for (int i = 0; i <= n; i ++) dp[j][i] = -inf; for (int i = 0; i <= n; i ++) dp[0][i] = 0; ///cout << cost(1, 1) << endl; for (int j = 1; j <= k; j ++) solve_layer(j, j, n, j, n); } void print_answer() { cout << dp[k % 2 ][n] << endl; int pos = n, part = k; vector < int > path; while(bef[part][pos] != 0) { pos = bef[part][pos]; part --; path.push_back(pos); } reverse(path.begin(), path.end()); for (int v : path) cout << v << " "; cout << endl; } void solve() { input(); calculate_prefix(); calculate_states(); print_answer(); } int main() { solve(); return 0; } /** 7 3 4 1 3 4 0 2 3 */
#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...