Submission #989567

#TimeUsernameProblemLanguageResultExecution timeMemory
989567_shr104Split the sequence (APIO14_sequence)C++17
49 / 100
71 ms131072 KiB
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; #define pb push_back #define pf push_front #define fi first #define se second const ll mod = 1e9+7, mxn = 1e5+7; ll n, k, dp[mxn][207], trace[mxn][207], a[mxn], sum = 0, all_pair = 0, ans = 0, pfs[mxn], pfs_2[mxn]; ll get(ll l, ll r) { ll ans = (pfs[r]-pfs[l-1])*(pfs[r]-pfs[l-1])-pfs_2[r]+pfs_2[l-1]; return ans>>1; } vector<vector<ll>> opt; void dnc(ll j, ll l, ll r, ll opt_l, ll opt_r) { ll m = (r+l)>>1; pair<ll,ll> res = {1e18, opt_l}; for (ll i = opt_l; i <= min(m,opt_r); i++) res = min(res,{dp[i-1][j-1]+get(i,m),i}); dp[m][j] = res.fi; trace[m][j] = res.se; if (l <= m-1) dnc(j,l,m-1,opt_l,res.se); if (m+1 <= r) dnc(j,m+1,r,res.se,opt_r); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("test.inp","r",stdin); freopen("test.out","w",stdout); freopen("test.err","w",stderr); cin >> n >> k; k++; for (ll i = 1; i <= n; i++) { cin >> a[i]; pfs[i] = pfs[i-1]+a[i]; pfs_2[i] = pfs_2[i-1]+a[i]*a[i]; } for (ll i = 1; i <= n; i++) {dp[i][1] = get(1,i);} for (ll i = 2; i <= k; i++) dnc(i,1,n,1,n); cout << get(1,n)-dp[n][k] << '\n'; vector<ll> v; while (k > 1) { v.pb(trace[n][k]-1); n = trace[n][k]-1; k--; } reverse(v.begin(),v.end()); for (ll i : v) cout << 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...