Submission #580018

#TimeUsernameProblemLanguageResultExecution timeMemory
580018leeh18Split the sequence (APIO14_sequence)C++17
50 / 100
1373 ms131072 KiB
// #pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; using ll = long long; #define sz(x) (int)size(x) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, a, b) for(int i = a; i < (b); ++i) typedef pair<int, int> pii; typedef vector<int> vi; // https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0200r0.html template<class Fun> class y_combinator_result { Fun fun_; public: template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {} template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); } }; template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); } mt19937 rng((unsigned)chrono::steady_clock::now().time_since_epoch().count()); struct Line { mutable ll k, m, p; int 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}; } }; void solve() { int n, k; cin >> n >> k; vector<ll> p(n+1); for (int i = 1; i <= n; i++) { cin >> p[i]; p[i] += p[i-1]; } vector<LineContainer> g(k); vector<vector<int>> parent(n+1, vector<int>(k+1)); vector<ll> dp(k+1); for (int i = 1; i <= n; i++) { dp[0] = 0; for (int j = 1; j < i && j <= k; j++) { tie(dp[j], parent[i][j]) = g[j-1].query(p[i]); } for (int j = 0; j < i && j < k; j++) { g[j].add(p[i], -p[i]*p[i] + dp[j], i); } } cout << dp[k] << '\n'; vector<int> result; int idx = n; for (int i = k; i >= 1; i--) { result.push_back(parent[idx][i]); idx = parent[idx][i]; } for (int i = k-1; i >= 0; i--) { cout << result[i] << ' '; } } signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int t = 1; while (t--) { solve(); } 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...