제출 #973425

#제출 시각아이디문제언어결과실행 시간메모리
973425TranGiaHuy1508수열 (APIO14_sequence)C++17
71 / 100
359 ms131072 KiB
#include <bits/stdc++.h> using namespace std; void main_program(); signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); main_program(); } using ll = long long; const ll inf = 1e18; struct Line { mutable ll k, m, p; bool operator<(const Line& o) const { return k < o.k; } bool operator<(ll x) const { return p < x; } }; struct LineContainer : multiset<Line, less<>> { // (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) { auto z = insert({k, m, 0}), 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)); } ll query(ll x) { assert(!empty()); auto l = *lower_bound(x); return l.k * x + l.m; } }; int n, k; vector<ll> v, pfx; void main_program(){ cin >> n >> k; v.resize(n + 1); for (int i = 1; i <= n; i++) cin >> v[i]; pfx.resize(n + 1); for (int i = 1; i <= n; i++) pfx[i] = pfx[i-1] + v[i]; vector<vector<ll>> dp(k + 2, vector<ll>(n + 1, -inf)); dp[0][0] = 0; for (int j = 1; j <= k + 1; j++){ LineContainer cht; for (int i = 1; i <= n; i++){ // Add line (pfx[i-1] * S + dp[i-1] - pfx[i-1]^2) cht.add(pfx[i-1], dp[j-1][i-1] - pfx[i-1] * pfx[i-1]); // Find answer dp[j][i] = cht.query(pfx[i]); } } cout << dp[k + 1][n] << "\n"; vector<int> splits; int crr = n; for (int j = k + 1; j > 0; j--){ for (int i = crr - 1; i >= 0; i--){ if (pfx[i] * pfx[crr] + dp[j-1][i] - pfx[i] * pfx[i] == dp[j][crr]){ crr = i; if (j > 1) splits.push_back(crr); break; } } } reverse(splits.begin(), splits.end()); for (auto elem: splits) cout << elem << " "; cout << "\n"; }
#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...