結果

問題 No.1117 数列分割
ユーザー keijakkeijak
提出日時 2022-01-01 00:50:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 665 ms / 3,000 ms
コード長 4,755 bytes
コンパイル時間 2,581 ms
コンパイル使用メモリ 206,784 KB
実行使用メモリ 74,240 KB
最終ジャッジ日時 2024-04-17 16:42:10
合計ジャッジ時間 9,805 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 32 ms
8,576 KB
testcase_04 AC 25 ms
7,936 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 29 ms
7,552 KB
testcase_09 AC 9 ms
5,376 KB
testcase_10 AC 29 ms
8,064 KB
testcase_11 AC 174 ms
22,016 KB
testcase_12 AC 162 ms
22,912 KB
testcase_13 AC 232 ms
30,720 KB
testcase_14 AC 248 ms
32,512 KB
testcase_15 AC 307 ms
38,912 KB
testcase_16 AC 357 ms
43,776 KB
testcase_17 AC 29 ms
7,808 KB
testcase_18 AC 639 ms
73,984 KB
testcase_19 AC 644 ms
74,240 KB
testcase_20 AC 303 ms
37,632 KB
testcase_21 AC 640 ms
74,240 KB
testcase_22 AC 629 ms
72,704 KB
testcase_23 AC 640 ms
72,704 KB
testcase_24 AC 629 ms
71,680 KB
testcase_25 AC 665 ms
71,680 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 16 ms
6,272 KB
testcase_28 AC 17 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<class T, class Container = std::vector<T>,
    class Compare = std::greater<typename Container::value_type>>
struct SlideMinQueue {
  Container vals;
  Compare compare;              // comparison funcation.
  int left, right;              // [left, right)
  std::deque<int> index_queue;  // indices where min values are stored.

  SlideMinQueue() : compare(), left(0), right(0) {}

  explicit SlideMinQueue(Container v)
      : vals(std::move(v)), compare(), left(0), right(0) {}

  // Shifts the window to the right.
  // Sets `left` to `l`, and `right` to `r`.
  void slide(int l, int r) {
    l = std::max(l, 0);
    r = std::min(r, (int) vals.size());
    assert(l >= left);
    assert(r >= right);
    for (int i = right; i < r; ++i) {
      push_right(i);
    }
    right = r;
    pop_left(l);
    left = l;
  }

  // Returns the minimum value in [left, right).
  T fold() const {
    int i = index_queue.front();
    return vals[i];
  }

  bool empty() const { return index_queue.empty(); }

 private:
  // Enqueues the i-th value.
  void push_right(int i) {
    while (!index_queue.empty() && compare(vals[index_queue.back()], vals[i])) {
      index_queue.pop_back();
    }
    index_queue.emplace_back(i);
  }

  // Dequeues indices less than i.
  void pop_left(int i) {
    while (!index_queue.empty() && index_queue.front() < i) {
      index_queue.pop_front();
    }
  }
};

template<class T, class Container = std::vector<T>>
using SlideMaxQueue = SlideMinQueue<T, Container, std::less<T>>;

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];
    }
    SlideMaxQueue<Int> rmq1(nv1), rmq2(nv2);
    REP(r, 1, n + 1) {
      {
        rmq1.slide(max(r - M, 0), r);
        auto v = rmq1.fold();
        chmax(dp[r][k], v + acc[r]);
      }
      {
        rmq2.slide(max(r - M, 0), r);
        auto v = rmq2.fold();
        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