Submission #306339

#TimeUsernameProblemLanguageResultExecution timeMemory
306339syySplit the sequence (APIO14_sequence)C++17
100 / 100
1895 ms88532 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define FOR(i, a, b) for(int i = (int)a; i <= (int)b; i++) #define DEC(i, a, b) for(int i = (int)a; i >= (int)b; i--) typedef pair<ll, ll> pi; typedef pair<pi, int> pii; #define f first #define s second typedef vector<int> vi; typedef vector<pi> vpi; typedef vector<pii> vpii; #define pb push_back #define pf push_front #define all(v) v.begin(), v.end() #define INF (int) 1e9 + 100 #define LLINF (ll) 1e18 #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) int n, k, ans[205][100005], arr[100005]; ll ss[100005]; inline ll cut(int x, int y, int m) { return (ss[y] - ss[m]) * (ss[m] - ss[x-1]); } deque <pii> dq; pi func(pii line, ll x) { return pi(line.f.f * x + line.f.s, line.s); } pi query(int x, int idx) { // Finds the maximum of a mathematical function while (dq.size() > 1) { if (dq[1].s > idx) break; if (func(dq[0], x).f < func(dq[1], x).f) dq.pop_front(); else break; } return func(dq[0], x); } double intersect(ll m1, ll c1, ll m2, ll c2) { return (double) (c1 - c2) / (double) (m2 - m1); } double intersect(pii p1, pii p2) { return intersect(p1.f.f, p1.f.s, p2.f.f, p2.f.s); } void insertline(ll m, ll c, int idx) { // y = mx + c pii line = pii(pi(m, c), idx); while (dq.size() > 1) { // To prevent seg fault ll si = dq.size(); if (intersect(dq[si - 1], line) <= intersect(dq[si - 2], line)) { // Criteria for useless line: intersect 2 <= intersect 1 dq.pop_back(); // Remove useless line } else break; } dq.push_back(line); } int main() { fastio; cin >> n >> k; FOR(i, 1, n) { cin >> arr[i]; ss[i] = ss[i-1] + arr[i]; } FOR(i, 0, k) { vpii toadd; FOR(j, i+1, n) { ll res; if (i == 0 or j == 1) res = 0; else { pi t = query(ss[j], j); res = t.f; ans[i][j] = t.s; } toadd.pb(pii(pi(ss[j], res - ss[j] * ss[j]), j)); if (i == k and j == n) cout << res << "\n"; } dq.clear(); for(auto it:toadd) insertline(it.f.f, it.f.s, it.s); } DEC(i, k, 1) { cout << ans[i][n] << " "; n = ans[i][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...