結果

問題 No.1117 数列分割
ユーザー keijakkeijak
提出日時 2022-01-01 03:00:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 439 ms / 3,000 ms
コード長 4,779 bytes
コンパイル時間 2,571 ms
コンパイル使用メモリ 210,668 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 19:31:47
合計ジャッジ時間 7,322 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 35 ms
5,376 KB
testcase_04 AC 30 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 25 ms
5,376 KB
testcase_09 AC 9 ms
5,376 KB
testcase_10 AC 25 ms
5,376 KB
testcase_11 AC 101 ms
5,376 KB
testcase_12 AC 133 ms
5,376 KB
testcase_13 AC 145 ms
5,376 KB
testcase_14 AC 159 ms
5,376 KB
testcase_15 AC 236 ms
5,376 KB
testcase_16 AC 266 ms
5,376 KB
testcase_17 AC 24 ms
5,376 KB
testcase_18 AC 357 ms
5,376 KB
testcase_19 AC 439 ms
5,376 KB
testcase_20 AC 211 ms
5,376 KB
testcase_21 AC 285 ms
5,376 KB
testcase_22 AC 359 ms
5,376 KB
testcase_23 AC 361 ms
5,376 KB
testcase_24 AC 353 ms
5,376 KB
testcase_25 AC 356 ms
5,376 KB
testcase_26 AC 2 ms
5,248 KB
testcase_27 AC 17 ms
5,376 KB
testcase_28 AC 17 ms
5,376 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 Monoid>
struct SWAGQueue {
  using T = typename Monoid::T;

  struct Node {
    T val, agg;
    Node(T val, T agg) : val(std::move(val)), agg(std::move(agg)) {}
  };
  std::stack<Node> front_stack, back_stack;

  SWAGQueue() = default;

  bool empty() const { return front_stack.empty() and back_stack.empty(); }

  int size() const { return front_stack.size() + back_stack.size(); }

  void push_back(const T &x) {
    if (back_stack.empty()) {
      back_stack.emplace(x, x);
    } else {
      back_stack.emplace(x, Monoid::op(back_stack.top().agg, x));
    }
  }

  void pop_front() {
    assert(!empty());
    if (front_stack.empty()) {
      front_stack.emplace(back_stack.top().val, back_stack.top().val);
      back_stack.pop();
      while (!back_stack.empty()) {
        T agg = Monoid::op(back_stack.top().val, front_stack.top().agg);
        front_stack.emplace(back_stack.top().val, std::move(agg));
        back_stack.pop();
      }
    }
    front_stack.pop();
  }

  T fold() const {
    if (empty()) return Monoid::id();
    if (front_stack.empty()) return back_stack.top().agg;
    if (back_stack.empty()) return front_stack.top().agg;
    return Monoid::op(front_stack.top().agg, back_stack.top().agg);
  }
};

const Int kBig = 1e16;

struct MaxOp {
  using T = long long;
  static T op(const T &x, const T &y) { return std::max(x, y); }
  static constexpr T id() { return -kBig; }
};

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(2, vector(n + 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[0][l];
      if (v == -kBig) continue;
      nv1[l] = v - acc[l];
      nv2[l] = v - acc_minus[l];
    }
    SWAGQueue<MaxOp> rmq1, rmq2;
    REP(r, 1, n + 1) {
      rmq1.push_back(nv1[r - 1]);
      while (rmq1.size() > M) rmq1.pop_front();
      if (auto v = rmq1.fold(); v != -kBig) {
        chmax(dp[1][r], v + acc[r]);
      }
      rmq2.push_back(nv2[r - 1]);
      while (rmq2.size() > M) rmq2.pop_front();
      if (auto v = rmq2.fold(); v != -kBig) {
        chmax(dp[1][r], v + acc_minus[r]);
      }
    }
    swap(dp[0], dp[1]);
    dp[1].assign(n + 1, -kBig);
  }
  return dp[0][n];
}

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