제출 #928355

#제출 시각아이디문제언어결과실행 시간메모리
928355HiepVu217수열 (APIO14_sequence)C++17
0 / 100
17 ms80508 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 17; int n, k, cut[217][N], z, as[N], c, it, sz; long long a[N], f[N], fz[N], s[N], ans; struct line { long long a, b; int c; } l, cv[N]; bool bad (line l1, line l2, line l3) { return 1.0 * (l1.b - l3.b) / (l3.a - l1.a) < 1.0 * (l1.b - l2.b) / (l2.a - l1.a); } long long calc (long long x, int i, int j) { while (it < sz) { if (cv[it].a * x + cv[it].b > cv[it + 1].a * x + cv[it + 1].b) { break; } ++it; } cut[j][i] = cv[it].c; return cv[it].a * x + cv[it].b; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; s[i] = s[i - 1] + a[i]; } for (int j = 2; j <= k + 1; ++j) { sz = 0, it = 1; for (int i = j - 1; i < n; ++i) { l = {- s[i - 1], fz[i - 1], i - 1}; fz[i - 1] = f[i - 1]; while (sz > 1) { if (bad (cv[sz - 1], cv[sz], l)) { --sz; continue; } break; } it = max (min (it, sz), 1); cv[++sz] = l; f[i] = calc (s[n] - s[i], i, j - 1) + s[i] * (s[n] - s[i]); if (f[i] >= ans) { ans = f[i]; z = i; } } } cout << ans << "\n"; if (!z) { return 0; } as[++c] = z; int x = cut[k][z]; while (k - 1) { as[++c] = x; x = cut[--k][x]; } while (c--) { cout << as[c + 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...