제출 #957318

#제출 시각아이디문제언어결과실행 시간메모리
957318vjudge1Split the sequence (APIO14_sequence)C++14
71 / 100
100 ms131072 KiB
#include<bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; #define int long long struct Line { mutable ll k, m, p, tr; bool operator<(const Line& o) const { return k < o.k; } bool operator<(ll x) const { return p < x; } }; struct LineContainer : vector<Line> { // (for doubles, use inf = 1/.0, div(a,b) = a/b) static const ll inf = LLONG_MAX; ll div(ll a, ll b) { // floored division return a / b - ((a ^ b) < 0 && a % b); } bool isect(iterator x, iterator y) { if (y == end()) return x->p = inf, 0; if (x->k == y->k) x->p = x->m > y->m ? inf : -inf; else x->p = div(y->m - x->m, x->k - y->k); return x->p >= y->p; } void add(ll k, ll m, int tr) { auto z = insert(end(), {k, m, 0, tr}), 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)); } int id=0; pair<int, int> query(ll x) { assert(!empty()); while ((begin()+id)->p<x) ++id; return {(begin()+id)->k * x + (begin()+id)->m, (begin()+id)->tr}; } }; const int N=1e5+10; int n, k, a[N], f[N][210], tr[N][210], pf[N]; LineContainer cht[210]; int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; ++k; for (int i=1; i<=n; ++i) cin >> a[i]; partial_sum(a, a+n+1, pf); cht[0].add(0, 0, 0); for (int i=1; i<=n; ++i){ for (int j=k; j>=1; --j) if (cht[j-1].size()){ auto tmp=cht[j-1].query(pf[i]); f[i][j]=tmp.first; tr[i][j]=tmp.second; cht[j].add(pf[i], f[i][j]-pf[i]*pf[i], i); } } cout << f[n][k] << '\n'; vector<int> ans; while (n){ if (tr[n][k]) ans.push_back(tr[n][k]); n=tr[n][k]; --k; } reverse(ans.begin(), ans.end()); for (int i:ans) cout << i << ' '; return 0; }
#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...