Submission #651486

#TimeUsernameProblemLanguageResultExecution timeMemory
651486pauloamedSplit the sequence (APIO14_sequence)C++17
100 / 100
1305 ms102576 KiB
#include<bits/stdc++.h> using namespace std; #define int long long const int MAXN = 100010; const int MAXK = 250; int S[MAXN], wS[MAXN]; int query(int l, int r){ int a = S[r]; int b = S[r] - ((l == 0)? 0 : S[l - 1]); int c = wS[r] - ((l == 0)? 0 : wS[l - 1]); return a * b - c; } int last_dp[MAXN]; int curr_dp[MAXN]; signed ans[MAXN][MAXK]; int get_dp(int i){ if(i < 0) return 0; else return last_dp[i]; } int K; void solve(int l, int r, int searchL, int searchR){ if(l > r) return; int j = (l + r) / 2; if(curr_dp[j] == -1){ int validR = min(j, searchR); pair<int,int> best = {LLONG_MAX, -1LL}; for(int k = searchL; k <= validR; ++k){ int x = query(k, j) + get_dp(k - 1); if(x <= best.first) best = {x, k}; } curr_dp[j] = best.first; ans[j][K] = best.second; solve(l, j - 1, searchL, best.second); solve(j + 1, r, best.second, searchR); } } int32_t main(){ int n, k; cin >> n >> k; vector<int> v(n); for(auto &x : v) cin >> x; int tot = 0; for(int i = 0; i < n; ++i){ S[i] = v[i]; if(i){ S[i] += S[i - 1]; tot += v[i] * S[i - 1]; } } for(int i = 0; i < n; ++i){ wS[i] = S[i] * v[i]; if(i) wS[i] += wS[i - 1]; } // for(int i = 0 ; i < n; ++i){ // for(int j = i; j < n; ++j){ // cout << i << " " << j << " : " << query(i, j) << "\n"; // } // } for(int i = 0; i < n; ++i){ last_dp[i] = query(0, i); } for(K = 0; K < k; ++K){ memset(curr_dp, -1, sizeof curr_dp); solve(0, n - 1, 0, n - 1); swap(curr_dp, last_dp); // for(int i = 0; i < n; ++i){ // cout << i << " : " << ans[i][k].first << " " << ans[i][k].second << "\n"; // } // cout << "=-=-=-=-=-=-\n"; } cout << tot - last_dp[n - 1] << "\n"; // cout << tot << " " << last_dp[n - 1] << "\n"; auto curr = n; for(int i = 0; i < k; ++i){ // nao eh isso o rte curr = ans[curr - 1][k - i - 1]; cout << curr << " "; } cout << "\n"; }
#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...