Submission #537247

#TimeUsernameProblemLanguageResultExecution timeMemory
537247Leonardo_PaesSplit the sequence (APIO14_sequence)C++17
89 / 100
1762 ms85716 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<int,pii> pip; #define f first #define s second const int maxn = 1e5+10, maxk = 210; ll dp[maxn][2], v[maxn], suf[maxn]; int opt[maxn][maxk]; void solve(int l, int r, int optl, int optr, int k){ if(l > r) return; int mid = (l + r) >> 1; dp[mid][1] = 0; for(int i=optl; i<=min(mid, optr); i++){ ll c = dp[i][0] + (suf[i+1] - suf[mid+1]) * suf[mid+1]; if(c > dp[mid][1]){ dp[mid][1] = c; opt[mid][k] = i; } } solve(l, mid-1, optl, opt[mid][k], k); solve(mid+1, r, opt[mid][k], optr, k); } int main(){ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); int n, k; cin >> n >> k; for(int i=1; i<=n; i++) cin >> v[i]; for(int i=n; i>=1; i--) suf[i] = v[i] + suf[i+1]; for(int i=1; i<n; i++) dp[i][0] = (suf[1] - suf[i+1]) * suf[i+1]; for(int i=2; i<=k; i++){ solve(1, n-1, 1, n-1, i); for(int j=1; j<=n; j++) dp[j][0] = dp[j][1]; } int id = 1; for(int i=2; i<n; i++) if(dp[i][0] > dp[id][0]) id = i; cout << dp[id][0] << "\n"; vector<int> ans; for(int i=k; i>=1; i--){ ans.push_back(id); id = opt[id][i]; } for(int i=0; i<ans.size(); i++){ if(ans[i] == 0) cout << 201 << "\n"; } for(int i=((int)ans.size())-1; i>=0; i--) cout << ans[i] << " "; cout << "\n"; return 0; /* 7 3 4 1 3 4 0 2 3 */ }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:46:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |  for(int i=0; i<ans.size(); i++){
      |               ~^~~~~~~~~~~
#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...