Submission #546275

#TimeUsernameProblemLanguageResultExecution timeMemory
546275JomnoiSplit the sequence (APIO14_sequence)C++17
100 / 100
760 ms81556 KiB
#include <bits/stdc++.h> #define DEBUG 0 using namespace std; const int MAX_N = 1e5 + 10; const int MAX_K = 2e2 + 10; const long long INF = 9e18 + 7; int A[MAX_N], pre[MAX_N]; int parent[MAX_K][MAX_N]; long long dp[2][MAX_N]; long long divide(const long long &a, const long long &b) { return a / b - ((a ^ b) < 0 and a % b != 0); } class Line { public : long long m, c; int idx; Line() {} Line(const long long &m_, const long long &c_, const int &i) : m(m_), c(c_), idx(i) {} long long dot(const long long &x) const { return m * x + c; } long long intersectX(const Line &o) const { return divide(o.c - c, m - o.m); } }; class LineContainer { private : deque <Line> hull; public : void init() { hull.clear(); } void addline(const Line &f) { if(!hull.empty() and hull.back().m == f.m and hull.back().c == f.c) { hull.pop_back(); } while(hull.size() >= 2 and f.intersectX(hull.back()) <= hull.back().intersectX(hull[hull.size() - 2])) { hull.pop_back(); } hull.push_back(f); } pair <long long, int> query(const long long &x) { while(hull.size() >= 2 and hull[0].dot(x) <= hull[1].dot(x)) { hull.pop_front(); } return make_pair(hull[0].dot(x), hull[0].idx); } }cht; int main() { int N, K; scanf(" %d %d", &N, &K); for(int i = 1; i <= N; i++) { scanf(" %d", &A[i]); pre[i] = pre[i - 1] + A[i]; } for(int i = 1; i < N; i++) { dp[1][i] = 1ll*(pre[N] - pre[i]) * pre[i]; } for(int i = 2; i <= K; i++) { int now = i % 2, prv = now ^ 1; cht.init(); for(int j = i; j < N; j++) { cht.addline(Line(pre[j - 1], 1ll*-pre[j - 1] * pre[N] + dp[prv][j - 1], j - 1)); auto [res, idx] = cht.query(pre[j]); dp[now][j] = res + 1ll*pre[j] * pre[N] - 1ll*pre[j] * pre[j]; parent[i][j] = idx; } } long long ans = -INF; int pos = N; for(int i = K; i < N; i++) { if(ans < dp[K % 2][i]) { ans = dp[K % 2][i]; pos = i; } } stack <int> stk; while(K > 0) { stk.emplace(pos); pos = parent[K--][pos]; } printf("%lld\n", ans); while(!stk.empty()) { printf("%d ", stk.top()); stk.pop(); } return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |     scanf(" %d %d", &N, &K);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
sequence.cpp:63:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |         scanf(" %d", &A[i]);
      |         ~~~~~^~~~~~~~~~~~~~
#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...