Submission #206945

#TimeUsernameProblemLanguageResultExecution timeMemory
206945ttnhuy313Split the sequence (APIO14_sequence)C++14
71 / 100
165 ms131076 KiB
#include <bits/stdc++.h> using namespace std; #define int long long typedef pair <int, int> ii; const int N = 1e5 + 5, oo = 1e18, K = 200; int a[N], suf[N], pref[N], n, dp[2][N], trace[N][K]; struct Line { int a, b, id; Line() {} Line(int _a, int _b, int _id) : a(_a), b(_b), id(_id) {} int val(int x) { return a * x + b; } }; struct CHT { vector <Line> st; CHT() {st.clear();} bool bad(Line a, Line b, Line c) { return (1.0 * (c.b - a.b) / (a.a - c.a) < 1.0 * (c.b - b.b) / (b.a - c.a)); } void addLine(Line d) { if (!st.empty() && d.a == st.back().a && d.b == st.back().b) return; while (st.size() >= 2 && bad(d, st.back(), st[st.size() - 2])) { // cerr << "delete Line y = " << st.back().a << "x + " << st.back().b << endl; st.pop_back(); } // cerr << "addLine y = " << d.a << "x + " << d.b << endl; st.push_back(d); } ii query(int x) { int l = 0, r = st.size() - 1, ans = -oo, id = -1; while (l <= r) { int mid = (l + r) >> 1; int cur = st[mid].val(x); if (ans < cur) ans = max(ans, cur), id = st[mid].id; if (mid != st.size() - 1 && st[mid + 1].val(x) > cur) l = mid + 1; else if (mid != 0 && st[mid - 1].val(x) > cur) r = mid - 1; else break; } return {ans, id}; } }; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int k; cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1; i <= n; ++i) { pref[i] = pref[i - 1] + a[i]; } memset(dp, -1, sizeof dp); for (int i = 1; i <= n; ++i) { dp[1][i] = pref[i] * (pref[n] - pref[i]); } int res = -1, id = -1; for (int t = 2; t <= k; ++t) { CHT Solver; int ff = t & 1; for (int i = 1; i <= n; ++i) { if (i >= t) { ii d = Solver.query(pref[i]); trace[i][t] = d.second; dp[ff][i] = d.first + pref[i] * (pref[n] - pref[i]); } if (t == k && res < dp[ff][i]) res = max(res, dp[ff][i]), id = i; // cerr << i << ' ' << t << ' ' << dp[ff][i] << endl; if (~dp[ff ^ 1][i]) Solver.addLine({pref[i], dp[ff ^ 1][i] - pref[i] * pref[n], i}); } memset(dp[ff ^ 1], -1, sizeof dp[ff ^ 1]); } if (k == 1) { int t = -1, dd = 0; for (int i = 1; i <= n; ++i) if (t < dp[1][i]) t = dp[1][i], dd = i; cout << t << endl << dd << endl; return 0; } cout << res << endl; vector <int> nn; nn.clear(); for (int i = k; i >= 1; --i) { nn.push_back(id); id = trace[id][i]; } reverse(nn.begin(), nn.end()); for (int v : nn) cout << v << ' '; return 0; }

Compilation message (stderr)

sequence.cpp: In member function 'ii CHT::query(long long int)':
sequence.cpp:40:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if (mid != st.size() - 1 && st[mid + 1].val(x) > cur) l = mid + 1;
        ~~~~^~~~~~~~~~~~~~~~
#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...