Submission #374252

#TimeUsernameProblemLanguageResultExecution timeMemory
374252KoDSplit the sequence (APIO14_sequence)C++17
100 / 100
942 ms82276 KiB
#include <bits/stdc++.h> template <class T> using Vec = std::vector<T>; using ll = long long; constexpr ll INF = 1000000000000000000 + 5; ll square(const ll x) { return x * x; } struct Line { ll a, b; Line() = default; ll eval(const ll x) const { return a * x + b; } }; bool is_fixed(const Line &l, const Line &m, const Line &n) { return __int128_t(m.b - l.b) * (m.a - n.a) < __int128_t(n.b - m.b) * (l.a - m.a); } int main() { int N, K; std::cin >> N >> K; Vec<int> A(N); for (auto &x: A) { std::cin >> x; } Vec<int> cum(N + 1); for (int i = 0; i < N; ++i) { cum[i + 1] = cum[i] + A[i]; } Vec<ll> dp(N + 1); for (int i = 0; i <= N; ++i) { dp[i] = square(cum[i]); } Vec<Vec<int>> index(K, Vec<int>(N + 1, -1)); for (int k = 0; k < K; ++k) { Vec<Line> line(N + 1); for (int i = 0; i <= N; ++i) { line[i].a = -2 * cum[i]; line[i].b = dp[i] + square(cum[i]); } std::fill(dp.begin(), dp.end(), INF); std::deque<int> active; for (int i = 0; i <= N; ++i) { if (!active.empty()) { const ll x = cum[i]; while (active.size() >= 2) { const auto &l = line[active[0]]; const auto &m = line[active[1]]; if (l.eval(x) >= m.eval(x)) { active.pop_front(); } else { break; } } index[k][i] = active[0]; dp[i] = line[active[0]].eval(x) + square(x); } while (active.size() >= 2) { const auto &l = line[active[active.size() - 2]]; const auto &m = line[active[active.size() - 1]]; const auto &n = line[i]; if (is_fixed(l, m, n)) { break; } else { active.pop_back(); } } active.push_back(i); } } std::cout << (square(cum[N]) - dp[N]) / 2 << '\n'; int i = N; Vec<int> split(K); for (int k = K - 1; k >= 0; --k) { i = index[k][i]; split[k] = i; } for (int k = 0; k < K; ++k) { std::cout << split[k] << " \n"[k + 1 == K]; } 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...