結果

問題 No.1117 数列分割
ユーザー keijakkeijak
提出日時 2022-01-01 00:42:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 559 ms / 3,000 ms
コード長 7,032 bytes
コンパイル時間 2,721 ms
コンパイル使用メモリ 210,672 KB
最終ジャッジ日時 2025-01-27 08:13:20
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0