Submission #989643

#TimeUsernameProblemLanguageResultExecution timeMemory
989643vjudge1Split the sequence (APIO14_sequence)C++17
0 / 100
20 ms4952 KiB
#include<bits/stdc++.h> #define pll pair<ll, ll> #define fi first #define se second #define ll long long #define pb push_back #define ar array #define vi vector #define bit(i,x) ((x >> i) & 1ll) #define all(x) x.begin(),x.end() #define low(l,r,x) x.begin()+l,x.begin()+r #define len(l,r) (r-l+1) #define PI acos(-1.0) template <class T> void setmin(T &a, T b) { if(b < a) a = b; } template <class T> void setmax(T &a, T b) { if(b > a) a = b; } const long long mod = 998244353, N = 2e5, INF = 1e18, Log = 19, MASK = 1 << 19; using namespace std; #define int long long vector<ll> dp_before, dp_cur; ll pfs[N+5] , perf[N+5]; ll n , k; ll C(ll i , ll j) { ll k = pfs[j] - pfs[i-1]; ll res = k*k; res -= (perf[j] - perf[i-1]); return res; } void compute(ll l,ll r, ll optl, ll optr) { if(l > r) return; ll mid = (l+r) >> 1; pll best = {INF,-1}; for(int i = optl; i <= min(mid,optr);i++) { best = min(best, {dp_before[i-1] + C(i,mid),i}); } if(best.se == -1) return; dp_cur[mid] = best.fi; int opt = best.se; compute(l,mid-1,optl,opt); compute(mid+1,r,opt,optr); } void kazuha() { cin >> n >> k; ll a[n+5]; pfs[0] = 0; for(int i = 1; i <= n;i++) { cin >> a[i]; pfs[i] = pfs[i-1] + a[i]; perf[i] = perf[i-1] + a[i]*a[i]; } ll ans = pfs[n]*pfs[n] - perf[n]; ans /= 2; dp_cur.assign(n+5,INF); dp_before.assign(n+5,INF); dp_before[0] = 0; dp_cur[0] = 0; for(int i = 1; i <= k+1;i++) { compute(1,n,1,n); dp_before = dp_cur; } ans = ans - dp_before[n]/2; cout << ans << '\n'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("permutation.cpp", "r", stdin); // freopen("permutation.cpp", "w", stdout); long long mitsu = 1; // cin >> mitsu; for(long long seele = 0; seele < mitsu;seele++) { // cout << "Case " << seele << " :"; kazuha(); } }
#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...