제출 #655290

#제출 시각아이디문제언어결과실행 시간메모리
655290benjaminkleyn수열 (APIO14_sequence)C++17
71 / 100
2101 ms89420 KiB
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> using namespace std; typedef long long ll; struct Line { mutable ll k, m, p, idx; bool operator<(const Line& o) const { return k < o.k; } bool operator<(ll x) const { return p < x; } }; struct LineContainer : 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->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 idx) { auto z = insert({k, m, 0, idx}), 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,int> query(ll x) { assert(!empty()); auto l = *lower_bound(x); return {l.k * x + l.m, l.idx}; } }; int N, K, a[100001]; ll pref[100001] = {0}; ll dp[100001][2] = {0}; int last_split[100001][201]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> N >> K; for (int i = 1; i <= N; i++) { cin >> a[i]; pref[i] = pref[i-1] + a[i]; } LineContainer cht; for (int k = 1; k <= K; k++) { cht.clear(); for (int i = k + 1; i <= N; i++) { dp[i][1] = 0; cht.add(pref[i-1], dp[i-1][0] - pref[i-1] * pref[i-1], i-1); auto [val, idx] = cht.query(pref[i]); dp[i][1] = val; last_split[i][k] = idx; } for (int i = 1; i <= N; i++) dp[i][0] = dp[i][1]; } cout << dp[N][0] << '\n'; int i = N; do { i = last_split[i][K]; cout << i << ' '; } while (--K); cout << '\n'; 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...