제출 #1207710

#제출 시각아이디문제언어결과실행 시간메모리
1207710jahongirSplit the sequence (APIO14_sequence)C++20
22 / 100
3 ms2632 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<typename T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag, tree_order_statistics_node_update>; #define ll long long #define pi pair<int,int> #define vi vector<int> #define pb push_back #define int ll #define all(a) a.begin(),a.end() struct Line{ mutable ll m,c,p, ind; bool operator<(const Line &o)const{return m<o.m;} bool operator<(ll x)const{return p<x;} }; struct CHT : multiset<Line,less<>>{ static const ll inf = LLONG_MAX; ll div(ll a, ll b){ return a/b - ((a^b)<0 && a%b); } bool isect(iterator x, iterator y){ if(y==end()) return x->p=inf,0; if(x->m==y->m) x->p = x->c > y->c ? inf : -inf; else x->p = div(x->c - y->c, y->m - x->m); return x->p>=y->p; } void add(ll m, ll c, ll ind){ m = -m, c = -c; auto z = insert({m,c,0,ind}), y = z++, x = y; while(isect(y,z)) z = erase(z); if(x != begin() && isect(--x,y)) isect(x,y=erase(y)); while((y=x) != begin() && (--x)->p >= y->p) isect(x,erase(y)); } pair<ll,ll> get(ll x){ assert(!empty()); auto l = *lower_bound(x); return {-(l.m * x + l.c),l.ind}; } }; const int mxn = 1e5+10; const ll inf = 1e18; int arr[mxn]; ll dp[mxn][201]; int suc[mxn][201]; void apply(ll &a, ll b){a=min(a,b);} void solve(){ int n,k; cin >> n >> k; CHT cht[k]; for(int i = 1; i <= n; i++){ cin >> arr[i]; arr[i] += arr[i-1]; dp[i][0] = arr[i]*arr[i]; cht[0].add(-2*arr[i],arr[i]*arr[i]+dp[i][0],i); } for(int i = 1; i < k; i++) cht[i].add(0,inf,-1); for(int i = 2; i <= n; i++) for(int j = min(i,k); j >= 1; j--){ auto [val,ind] = cht[j-1].get(arr[i]); dp[i][j] = val + arr[i]*arr[i]; suc[i][j] = ind; if(j!=k) cht[j].add(-2*arr[i],arr[i]*arr[i] + dp[i][j],i); } cout << (arr[n]*arr[n] - dp[n][k])/2 << '\n'; vector<int> res; for(int u = suc[n][k], cnt = k; cnt>0; u = suc[u][--cnt]) res.pb(u); sort(all(res)); res.erase(unique(all(res)),res.end()); for(auto x : res) cout << x << ' '; } signed main(){ cin.tie(0)->sync_with_stdio(0); int t = 1; while(t--) solve(); }
#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...