제출 #957328

#제출 시각아이디문제언어결과실행 시간메모리
957328vjudge1수열 (APIO14_sequence)C++14
0 / 100
75 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 int 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], pf[N], tr[N][210]; LineContainer cht[2]; 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); int res=0; for (int j=1; j<=k; ++j){ for (int i=1; i<=n; ++i){ auto tmp=cht[0].query(pf[i]); if (i==n && j==k) res=tmp.first; tr[i][j]=tmp.second; cht[1].add(pf[i], tmp.first-pf[i]*pf[i], i); } cht[1].swap(cht[0]); cht[1].clear(); } cout << res << '\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...