Submission #546204

#TimeUsernameProblemLanguageResultExecution timeMemory
546204JomnoiSplit the sequence (APIO14_sequence)C++17
11 / 100
854 ms81852 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]; long long pre[MAX_N], dp[2][MAX_N]; int parent[MAX_K][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 : mutable long long m, c; mutable int idx; Line() : m(0), c(-INF), idx(0) {} 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) { 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[0].m == f.m and hull[0].c == f.c) { hull.pop_front(); } while(hull.size() >= 2 and f.intersectX(hull[0]) <= hull[0].intersectX(hull[1])) { hull.pop_front(); } hull.push_front(f); } pair <long long, int> query(const long long &x) { while(hull.size() >= 2 and x >= hull.back().intersectX(hull[hull.size() - 2])) { hull.pop_back(); } return make_pair(hull.back().dot(x), hull.back().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] = (pre[N] - pre[i]) * pre[i]; } for(int i = 2; i <= K; i++) { cht.init(); int now = i % 2; int prv = now ^ 1; for(int j = i; j < N; j++) { cht.addline(Line(pre[j - 1], -pre[j - 1] * pre[N] + dp[prv][j - 1], j - 1)); auto [res, idx] = cht.query(pre[j]); dp[now][j] = res + pre[j] * pre[N] - 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...