結果

問題 No.1117 数列分割
ユーザー keijakkeijak
提出日時 2022-01-01 00:42:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 466 ms / 3,000 ms
コード長 7,032 bytes
コンパイル時間 2,127 ms
コンパイル使用メモリ 213,272 KB
実行使用メモリ 74,240 KB
最終ジャッジ日時 2024-04-17 16:33:20
合計ジャッジ時間 7,364 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 21 ms
8,832 KB
testcase_04 AC 21 ms
8,320 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 22 ms
7,296 KB
testcase_09 AC 7 ms
5,376 KB
testcase_10 AC 22 ms
7,552 KB
testcase_11 AC 125 ms
21,504 KB
testcase_12 AC 137 ms
23,424 KB
testcase_13 AC 172 ms
30,592 KB
testcase_14 AC 192 ms
32,640 KB
testcase_15 AC 229 ms
38,912 KB
testcase_16 AC 286 ms
44,160 KB
testcase_17 AC 27 ms
7,936 KB
testcase_18 AC 311 ms
74,112 KB
testcase_19 AC 397 ms
74,240 KB
testcase_20 AC 244 ms
37,248 KB
testcase_21 AC 451 ms
74,240 KB
testcase_22 AC 448 ms
72,960 KB
testcase_23 AC 456 ms
72,704 KB
testcase_24 AC 440 ms
71,808 KB
testcase_25 AC 466 ms
71,680 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 10 ms
6,272 KB
testcase_28 AC 10 ms
6,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP_(i, a_, b_, a, b, ...) for (int i = (a), END_##i = (b); i < END_##i; ++i)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(x) std::begin(x), std::end(x)
using Int = long long;
using Uint = unsigned long long;
using Real = long double;

template<typename T, typename U>
inline bool chmax(T &a, U b) {
  return a < b and ((a = std::move(b)), true);
}
template<typename T, typename U>
inline bool chmin(T &a, U b) {
  return a > b and ((a = std::move(b)), true);
}
template<typename T>
inline int ssize(const T &a) {
  return (int) a.size();
}

struct Void {};

template<class T>
inline std::ostream &print_one(const T &x, char endc) {
  if constexpr (std::is_same<T, Void>::value) {
    return std::cout;  // print nothing
  } else if constexpr (std::is_same<T, bool>::value) {
    return std::cout << (x ? "Yes" : "No") << endc;
  } else {
    return std::cout << x << endc;
  }
}
template<class T>
inline std::ostream &print(const T &x) { return print_one(x, '\n'); }
template<typename T, typename... Ts>
std::ostream &print(const T &head, Ts... tail) {
  return print_one(head, ' '), print(tail...);
}
inline std::ostream &print() { return std::cout << '\n'; }

template<typename Container>
std::ostream &print_seq(const Container &seq,
                        const char *sep = " ",
                        const char *ends = "\n",
                        std::ostream &os = std::cout) {
  const auto itl = std::begin(seq), itr = std::end(seq);
  for (auto it = itl; it != itr; ++it) {
    if (it != itl) os << sep;
    os << *it;
  }
  return os << ends;
}

struct CastInput {
  template<typename T>
  operator T() const {
    T x;
    std::cin >> x;
    return x;
  }
  struct Sized {
    std::size_t n;
    template<typename T>
    operator T() const {
      T x(n);
      for (auto &e: x) std::cin >> e;
      return x;
    }
  };
  Sized operator()(std::size_t n) const { return {n}; }
} in;

#ifdef MY_DEBUG
#include "debug_dump.hpp"
#include "backward.hpp"
backward::SignalHandling kSignalHandling;
#else
#define DUMP(...)
#define cerr if(false)cerr
#endif

using namespace std;

template<typename T>
constexpr int num_bits = CHAR_BIT * sizeof(T);

inline int msb_log(unsigned x) {
  assert(x != 0);
  return num_bits<unsigned> - __builtin_clz(x) - 1;
}

template<class BetterOp, class mask_t = unsigned>
struct RMQ {
  static_assert(std::is_integral_v<mask_t>, "mask_t must be integral");
  static_assert(std::is_unsigned_v<mask_t>, "mask_t must be unsigned");
  static_assert(std::is_invocable_r_v<bool, BetterOp, int, int>);
  static constexpr int block_size_ = num_bits<mask_t>;

  int n_;                 // sequence size
  int block_count_;       // total number of blocks
  BetterOp better_than_;  // checks if lhs is strictly better than rhs.
  std::vector<mask_t> indicator_;
  std::vector<std::vector<int>> sparse_table_;

  RMQ(int n, BetterOp better)
      : n_(n),
        block_count_((n_ + block_size_ - 1) / block_size_),
        better_than_(std::move(better)),
        indicator_(n_),
        sparse_table_(
            block_count_ == 0 ? 0 : msb_log(unsigned(block_count_)) + 1,
            std::vector<int>(block_count_)) {
    static constexpr int bufsize = block_size_ + 1;
    static int buf[bufsize];       // ring buffer [lp,rp)
    int lp = 1, rp = 1, rpm1 = 0;  // rpm1 = rp-1 (mod bufsize)

    // Build the indicator table.
    for (int r = 0; r < n_; ++r) {
      while (lp != rp and r - buf[lp] >= block_size_) {
        ++lp;
        if (lp == bufsize) lp = 0;
      }
      while (lp != rp and not better_than_(buf[rpm1], r)) {
        rp = rpm1--;
        if (rp == 0) rpm1 = bufsize - 1;
      }
      indicator_[r] = 1;
      if (lp != rp) {
        const int p = buf[rpm1];
        indicator_[r] |= (indicator_[p] << (r - p));
      }
      buf[rp] = r;
      rpm1 = rp++;
      if (rp == bufsize) rp = 0;
    }

    // Build the sparse table.
    for (int i = 0; i < block_count_; ++i) {
      sparse_table_[0][i] =
          best_index_small(std::min(block_size_ * (i + 1), n_) - 1);
    }
    for (int i = 0, height = int(sparse_table_.size()) - 1; i < height; ++i) {
      for (int j = 0; j < block_count_; ++j) {
        sparse_table_[i + 1][j] = better_index(
            sparse_table_[i][j],
            sparse_table_[i][std::min(j + (1 << i), block_count_ - 1)]);
      }
    }
  }

  // Returns the index of the best value in [l, r) (half-open interval).
  inline int fold(int l, int r) const {
    assert(l < r);
    // Internally use closed interval.
    return best_index(l, r - 1);
  }

 private:
  inline int better_index(int i, int j) const {
    return better_than_(i, j) ? i : j;
  }

  // Returns the index of the best value in [r - width, r] (closed interval).
  inline int best_index_small(int r, int width = block_size_) const {
    assert(r < n_);
    assert(width > 0);
    assert(width <= block_size_);
    mask_t ind = indicator_[r];
    if (width < block_size_) {
      ind &= (mask_t(1) << width) - 1;
    }
    return r - msb_log(ind);
  }

  // Returns the index of the best value in [l, r] (closed interval).
  inline int best_index(int l, int r) const {
    l = std::clamp(l, 0, n_ - 1);
    r = std::clamp(r, 0, n_ - 1);
    const int width = r - l + 1;
    if (width <= block_size_) {
      return best_index_small(r, width);
    }
    const int al = best_index_small(std::min(l + block_size_, n_) - 1);
    const int ar = best_index_small(r);
    int ans = better_index(al, ar);

    const int bl = l / block_size_ + 1;
    const int br = r / block_size_ - 1;
    if (bl <= br) {
      const int k = msb_log(unsigned(br - bl + 1));
      const int bl2 = br - (1 << k) + 1;
      const int am = better_index(sparse_table_[k][bl], sparse_table_[k][bl2]);
      ans = better_index(ans, am);
    }
    return ans;
  }
};

const Int kBig = 1e16;

auto solve() {
  int n = in, K = in, M = in;
  vector<Int> a = in(n);
  vector<Int> acc(n + 1);
  REP(i, n) acc[i + 1] = acc[i] + a[i];
  vector<Int> acc_minus(n + 1);
  REP(i, n + 1) acc_minus[i] = -acc[i];

  auto dp = vector(n + 1, vector(K + 1, -kBig));
  dp[0][0] = 0;

  REP(k, 1, K + 1) {
    vector<Int> nv1(n + 1, -kBig), nv2(n + 1, -kBig);
    for (int l = 0; l <= n; ++l) {
      Int v = dp[l][k - 1];
      if (v == -kBig) continue;
      nv1[l] = v - acc[l];
      nv2[l] = v - acc_minus[l];
    }
    RMQ rmq1(n + 1, [&](int i, int j) { return nv1[i] > nv1[j]; });
    RMQ rmq2(n + 1, [&](int i, int j) { return nv2[i] > nv2[j]; });

    REP(r, 1, n + 1) {
      if (Int v = rmq1.fold(max(r - M, 0), r); nv1[v] != -kBig) {
        chmax(dp[r][k], nv1[v] + acc[r]);
      }
      if (Int v = rmq2.fold(max(r - M, 0), r); nv2[v] != -kBig) {
        chmax(dp[r][k], nv2[v] + acc_minus[r]);
      }
    }
  }
  return dp[n][K];
}

int main() {
  std::ios::sync_with_stdio(false), cin.tie(nullptr);
  cout << std::fixed << std::setprecision(18);
  const int T = 1;//in;
  REP(t, T) {
    auto ans = solve();
    print(ans);
  }
}
0