제출 #725224

#제출 시각아이디문제언어결과실행 시간메모리
725224colossal_pepe수열 (APIO14_sequence)C++17
71 / 100
168 ms131072 KiB
#include <iostream> #include <tuple> #include <vector> #include <algorithm> using namespace std; using ll = long long; using line = tuple<ll, ll, int>; const int N = 1e5; const int K = 200; int n, k; ll a[N + 5], pfx_sum[N + 5]; pair<ll, int> dp[K + 5][N + 5]; vector<line> v1, v2; ll ps(int i) { return pfx_sum[i]; } ll ss(int i) { return pfx_sum[n] - pfx_sum[i - 1]; } ll f(line l, ll x) { auto [m, c, i] = l; return m * x + c; } bool lastIsBad(line lz, line ly, line lx) { auto [mz, cz, iz] = lz; auto [my, cy, iy] = ly; auto [mx, cx, ix] = lx; return (cy - cx) * (mx - mz) <= (cz - cx) * (mx - my); // ((cy - cx) / (mx - my)) <= ((cz - cx) / (mx - mz)) } pair<ll, vector<int>> retrieve() { ll x = -1; vector<int> v = {-1}; for (int i = 1; i <= n; i++) { if (dp[k][i].first > x) { x = dp[k][i].first; v.back() = i; } } for (int i = k; i >= 2; i--) { v.push_back(dp[i][v.back()].second); } return {x, v}; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; a[0] = pfx_sum[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; pfx_sum[i] = a[i] + pfx_sum[i - 1]; } for (int s = 1; s <= k; s++) { v2.clear(); for (int i = 1; i <= n; i++) { dp[s][i] = {-1, -1}; while (v1.size() > 1 and f(v1[v1.size() - 1], ss(i + 1)) <= f(v1[v1.size() - 2], ss(i + 1)) and get<2>(v1[v1.size() - 2]) < i) v1.pop_back(); if (s == 1) dp[s][i].first = ps(i) * ss(i + 1); else if (not v1.empty() and get<2>(v1[v1.size() - 1]) < i) { dp[s][i].first = ps(i) * ss(i + 1) + f(v1[v1.size() - 1], ss(i + 1)); dp[s][i].second = get<2>(v1[v1.size() - 1]); } // cout << s << ' ' << i << ' ' << dp[s][i].first << ' ' << dp[s][i].second << endl; if (s != 1 and dp[s][i].first == -1) continue; line cur = make_tuple(-ps(i), dp[s][i].first, i); while (v2.size() > 1 and lastIsBad(v2[v2.size() - 1], v2[v2.size() - 2], cur)) v2.pop_back(); v2.push_back(cur); } reverse(v2.begin(), v2.end()); swap(v1, v2); } pair<ll, vector<int>> ans = retrieve(); cout << ans.first << endl; for (int i : ans.second) { cout << i << ' '; } cout << endl; 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...