Submission #259097

# Submission time Handle Problem Language Result Execution time Memory
259097 2020-08-07T07:53:53 Z KoD Split the sequence (APIO14_sequence) C++17
100 / 100
893 ms 86888 KB
#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

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 time Memory Grader output
1 Correct 0 ms 256 KB contestant found the optimal answer: 108 == 108
2 Correct 0 ms 256 KB contestant found the optimal answer: 999 == 999
3 Correct 0 ms 256 KB contestant found the optimal answer: 0 == 0
4 Correct 0 ms 256 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 0 ms 256 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 1 ms 256 KB contestant found the optimal answer: 1 == 1
7 Correct 0 ms 256 KB contestant found the optimal answer: 1 == 1
8 Correct 0 ms 256 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 256 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 0 ms 256 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 1 ms 256 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 0 ms 256 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 256 KB contestant found the optimal answer: 140072 == 140072
14 Correct 0 ms 256 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 0 ms 256 KB contestant found the optimal answer: 805 == 805
16 Correct 0 ms 256 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 0 ms 256 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 0 ms 256 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 384 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 256 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 256 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 0 ms 256 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 384 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 0 ms 384 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 384 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 256 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 0 ms 384 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 1 ms 512 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 384 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 1 ms 384 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 1 ms 512 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 1 ms 512 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 1 ms 384 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 1 ms 384 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 1 ms 384 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 1 ms 384 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 7 ms 1152 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 1 ms 384 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 8 ms 1152 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 8 ms 1024 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 7 ms 1152 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 7 ms 1152 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 2 ms 512 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 3 ms 640 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1280 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 4 ms 1280 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 69 ms 8960 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 6 ms 1280 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 55 ms 6052 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 61 ms 6528 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 61 ms 7040 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 55 ms 6016 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 57 ms 6592 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 72 ms 8192 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 43 ms 9712 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 42 ms 9712 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 754 ms 86888 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 55 ms 10344 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 893 ms 86632 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 624 ms 62952 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 649 ms 68200 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 530 ms 57064 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 596 ms 63208 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 701 ms 78952 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845