結果

問題 No.1117 数列分割
ユーザー keijakkeijak
提出日時 2022-01-01 00:36:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,738 bytes
コンパイル時間 2,655 ms
コンパイル使用メモリ 207,784 KB
実行使用メモリ 74,496 KB
最終ジャッジ日時 2024-04-17 16:25:41
合計ジャッジ時間 13,627 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
5,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

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<class BetterOp>
struct SparseTableRMQ {
  static_assert(std::is_invocable_r_v<bool, BetterOp, int, int>);

  int n_;                 // sequence size
  BetterOp better_than_;  // checks if lhs is strictly better than rhs.
  std::vector<std::vector<int>> sparse_table_;

  SparseTableRMQ(int n, BetterOp better)
      : n_(n),
        better_than_(std::move(better)),
        sparse_table_(n == 0 ? 1 : (msb_log(n) + 1), std::vector<int>(n)) {
    for (int j = 0; j < n; ++j) {
      sparse_table_[0][j] = j;
    }
    for (int i = 0, height = int(sparse_table_.size()) - 1; i < height; ++i) {
      for (int j = 0; j < n; ++j) {
        sparse_table_[i + 1][j] =
            better_index(sparse_table_[i][j],
                         sparse_table_[i][std::min(j + (1 << i), n - 1)]);
      }
    }
  }

  // Returns the index of the best value in [l, r) (half-open interval).
  inline int fold(int l, int r) const {
    assert(0 <= l and l < r);
    assert(1 <= r and r <= n_);
    const int k = msb_log(r - l);
    const int l2 = r - (1 << k);
    return better_index(sparse_table_[k][l], sparse_table_[k][l2]);
  }

 private:
  // Log base 2 of the most significant bit that is set to 1.
  static inline int msb_log(unsigned x) {
    return std::numeric_limits<unsigned>::digits - __builtin_clz(x) - 1;
  }

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

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];
    }
    SparseTableRMQ rmq1(n + 1, [&](int i, int j) { return nv1[i] > nv1[j]; });
    SparseTableRMQ 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); v != -kBig) {
        chmax(dp[r][k], v + acc[r]);
      }
      if (Int v = rmq2.fold(max(r - M, 0), r); v != -kBig) {
        chmax(dp[r][k], 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