Submission #259097

#TimeUsernameProblemLanguageResultExecution timeMemory
259097KoDSplit the sequence (APIO14_sequence)C++17
100 / 100
893 ms86888 KiB
#line 1 "main.cpp" /** * @title Template */ #include <iostream> #include <algorithm> #include <utility> #include <numeric> #include <vector> #include <array> #include <deque> #line 2 "/Users/kodamankod/Desktop/Programming/Library/other/chmin_chmax.cpp" template <class T, class U> constexpr bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return true; } return false; } template <class T, class U> constexpr bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return true; } return false; } /** * @title Chmin/Chmax */ #line 2 "/Users/kodamankod/Desktop/Programming/Library/other/range.cpp" #line 4 "/Users/kodamankod/Desktop/Programming/Library/other/range.cpp" class range { public: class iterator { private: int64_t M_position; public: constexpr iterator(int64_t position) noexcept: M_position(position) { } constexpr void operator ++ () noexcept { ++M_position; } constexpr bool operator != (iterator other) const noexcept { return M_position != other.M_position; } constexpr int64_t operator * () const noexcept { return M_position; } }; class reverse_iterator { private: int64_t M_position; public: constexpr reverse_iterator(int64_t position) noexcept: M_position(position) { } constexpr void operator ++ () noexcept { --M_position; } constexpr bool operator != (reverse_iterator other) const noexcept { return M_position != other.M_position; } constexpr int64_t operator * () const noexcept { return M_position; } }; private: const iterator M_first, M_last; public: constexpr range(int64_t first, int64_t last) noexcept: M_first(first), M_last(std::max(first, last)) { } constexpr iterator begin() const noexcept { return M_first; } constexpr iterator end() const noexcept { return M_last; } constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(*M_last - 1); } constexpr reverse_iterator rend() const noexcept { return reverse_iterator(*M_first - 1); } }; /** * @title Range */ #line 2 "/Users/kodamankod/Desktop/Programming/Library/other/rev.cpp" #include <type_traits> #include <iterator> #line 6 "/Users/kodamankod/Desktop/Programming/Library/other/rev.cpp" template <class T> class rev_impl { public: using iterator = typename std::decay<T>::type::reverse_iterator; private: const iterator M_begin; const iterator M_end; public: constexpr rev_impl(T &&cont) noexcept: M_begin(std::rbegin(cont)), M_end(std::rend(cont)) { } constexpr iterator begin() const noexcept { return M_begin; } constexpr iterator end() const noexcept { return M_end; } }; template <class T> constexpr decltype(auto) rev(T &&cont) { return rev_impl<T>(std::forward<T>(cont)); } /** * @title Reverser */ #line 17 "main.cpp" using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; constexpr i32 inf32 = (i32(1) << 30) - 1; constexpr i64 inf64 = (i64(1) << 62) - 1; struct line_t { i64 a, b; i32 idx; i64 val(i64 x) const { return a * x + b; } }; struct convex_hull_trick { std::deque<line_t> lines; bool need(line_t a, line_t b, line_t c) const { i64 x1 = (c.b - b.b) * (a.a - b.a); i64 x2 = (b.b - a.b) * (b.a - c.a); return x1 < x2; } bool empty() const { return lines.empty(); } void add_line(line_t l) { i32 size = lines.size(); while (size >= 2 && !need(l, lines[size - 1], lines[size - 2])) { lines.pop_back(); --size; } lines.push_back(l); } std::pair<i64, i32> min(i64 x) { i32 size = lines.size(); while (size >= 2 && lines[0].val(x) > lines[1].val(x)) { lines.pop_front(); --size; } return { lines[0].val(x), lines[0].idx }; } }; int main() { i32 N, K; std::cin >> N >> K; ++K; std::vector<i32> A(N); for (auto &x: A) { std::cin >> x; } std::vector<i64> sum(N + 1); for (auto i: range(0, N)) { sum[i + 1] = sum[i] + A[i]; } std::vector<i64> dp(N + 1, inf64); std::vector<std::vector<i32>> memo(N + 1, std::vector<i32>(K + 1, -1)); dp[0] = 0; for (auto k: range(1, K + 1)) { convex_hull_trick cht; std::vector<i64> next(N + 1, inf64); for (auto i: range(1, N + 1)) { if (dp[i - 1] != inf64) { cht.add_line(line_t{ -2 * sum[i - 1], sum[i - 1] * sum[i - 1] + dp[i - 1], i - 1 }); } if (!cht.empty()) { auto [v, j] = cht.min(sum[i]); next[i] = v + sum[i] * sum[i]; memo[i][k] = j; } } dp = std::move(next); } std::cout << ((sum[N] - sum[0]) * (sum[N] - sum[0]) - dp[N]) / 2 << '\n'; std::vector<i32> step; { i32 r = N, k = K; while (r > 0) { r = memo[r][k]; step.push_back(r); --k; } } step.pop_back(); for (auto i: range(0, step.size())) { std::cout << step[i] << (i + 1 == step.size() ? '\n' : ' '); } return 0; }

Compilation message (stderr)

main.cpp: In function 'int main()':
main.cpp:82:86: warning: narrowing conversion of '(i - 1)' from 'long int' to 'i32 {aka int}' inside { } [-Wnarrowing]
main.cpp:104:36: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#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...