Submission #1104474

#TimeUsernameProblemLanguageResultExecution timeMemory
1104474BF001Split the sequence (APIO14_sequence)C++17
0 / 100
13 ms9808 KiB
#include<bits/stdc++.h> using namespace std; #define fi first #define se second #define N 100005 #define oo 1e15 #define int long long typedef pair<int, long long> ii; typedef pair<int, pair<long long, long long>> iii; struct cht{ deque<iii> q; long long get(ii tmp, long long x){ return tmp.fi * x + tmp.se; } bool cal(ii a, ii b, ii c){ return ((long long) 1ll * (c.se - a.se) * (b.fi - c.fi) >= 1ll * (c.se - b.se) * (a.fi - c.fi)); } void add(long long a, long long b, int pos){ iii nw = {pos, {a, b}}; while ((int) q.size() >= 2 && cal(q[(int)q.size() - 2].se, q[(int)q.size() - 1].se, nw.se)) q.pop_back(); q.push_back(nw); } ii get(long long pos){ if (!(int) q.size()) return {0, 0}; while ((int) q.size() >= 2 && get(q[0].se, pos) <= get(q[1].se, pos)) q.pop_front(); return {q[0].fi, get(q[0].se, pos)}; } void reset(){ q.clear(); } } c1; long long pre[N], suf[N]; int par[201][N], n, k; vector<long long> cur, lst; signed main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> k; for (int i = 1; i <= n; i++){ cin >> pre[i]; pre[i] = pre[i - 1] + pre[i]; } for (int i = 1; i <= n; i++){ suf[i] = pre[n] - pre[i - 1]; } cur.resize(n + 1, 0); lst.resize(n + 1, 0); for (int i = 1; i <= n; i++){ cur[i] = pre[i] * suf[i + 1]; } for (int i = 2; i <= k; i++){ lst = cur; c1.reset(); for (int j = 1; j <= n; j++){ ii gt = c1.get(-suf[j + 1]); par[i][j] = gt.fi; cur[j] = gt.se + suf[j + 1] * pre[j]; c1.add(pre[j], lst[j], j); } } long long res = -oo; int pos = 0, nn = k; for (int i = 1; i <= n; i++){ if (cur[i] > res){ res = cur[i]; pos = i; } } vector<int> vec; for (int i = k; i >= 1; i--){ vec.push_back(par[i][pos]); pos = par[i][pos]; } sort(vec.begin(), vec.end()); cout << res << "\n"; for (auto x : vec) cout << x << " "; return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:71:39: warning: unused variable 'nn' [-Wunused-variable]
   71 |     long long res = -oo; int pos = 0, nn = k;
      |                                       ^~
#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...