結果

問題 No.1117 数列分割
ユーザー pentapenta
提出日時 2020-07-25 17:51:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 717 ms / 3,000 ms
コード長 3,324 bytes
コンパイル時間 2,367 ms
コンパイル使用メモリ 212,348 KB
実行使用メモリ 73,980 KB
最終ジャッジ日時 2023-09-09 18:18:20
合計ジャッジ時間 9,930 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 40 ms
8,664 KB
testcase_04 AC 35 ms
8,292 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 31 ms
7,612 KB
testcase_09 AC 10 ms
4,684 KB
testcase_10 AC 33 ms
7,744 KB
testcase_11 AC 157 ms
21,832 KB
testcase_12 AC 181 ms
23,184 KB
testcase_13 AC 236 ms
30,944 KB
testcase_14 AC 242 ms
32,568 KB
testcase_15 AC 325 ms
38,700 KB
testcase_16 AC 374 ms
43,804 KB
testcase_17 AC 30 ms
7,672 KB
testcase_18 AC 326 ms
73,548 KB
testcase_19 AC 598 ms
73,548 KB
testcase_20 AC 300 ms
37,456 KB
testcase_21 AC 647 ms
73,980 KB
testcase_22 AC 655 ms
73,012 KB
testcase_23 AC 637 ms
73,200 KB
testcase_24 AC 635 ms
71,852 KB
testcase_25 AC 717 ms
71,988 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 11 ms
6,160 KB
testcase_28 AC 11 ms
6,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep2(i,m,n) for (int i = (m); i < (n); ++i)
#define rep3(i,a,b) for (int i = (a); i >= (b); --i)
#define all(x) (x).begin(),(x).end()
inline int popcount(const int x) { return __builtin_popcount(x);}
inline ll popcount(const ll x) { return __builtin_popcountll(x);}
template<class T> void chmin(T &a, const T &b) noexcept { if (b < a) a = b;}
template<class T> void chmax(T &a, const T &b) noexcept { if (a < b) a = b;}
template<class T> void drop(const T &x) { std::cout<<x<<endl; exit(0);}
void debug_out() { std::cout << "\n";}
template<class T, class... Args> void debug_out(const T &x, const Args &... args) { std::cout<<x<< " "; debug_out(args...);}
#ifdef _DEBUG
  #define debug(...) debug_out(__VA_ARGS__)
#else
  #define debug(...) 
#endif

struct InitIO{
  InitIO() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
  }
}init_io;


template<class T, class F> struct queue_aggression {
private:
  // using F = function<T(T,T)>;
  struct node {
    T val, sum;
    node(const T &val, const T &sum):val(val),sum(sum){}
  };
  const F f;
  stack<node> front_stack, back_stack;

public:
  queue_aggression(const F &f):f(f){}
  bool empty() const { return front_stack.empty() && back_stack.empty();}
  int size() const { return front_stack.size() + back_stack.size();}

  T fold_all() {
    assert(!empty());
    if (front_stack.empty()) return back_stack.top().sum;
    else if (back_stack.empty()) return front_stack.top().sum;
    else return f(front_stack.top().sum, back_stack.top().sum);
  }
  void push(const T &x) {
    if (back_stack.empty()) back_stack.emplace(x, x);
    else {
      T s = f(back_stack.top().sum, x);
      back_stack.emplace(x, s);
    }
  }
  void pop() {
    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 s = f(back_stack.top().val, front_stack.top().sum);
        front_stack.emplace(back_stack.top().val, s);
        back_stack.pop();
      }
    }
    front_stack.pop();
  }
};

const ll INF = 1LL<<60;

int main() {
  int n, k, m;
  cin >> n >> k >> m;
  vector<ll> a(n), s(n+1, 0);
  rep(i,n) {
    cin >> a[i];
    s[i+1] += s[i] + a[i];
  }
  vector<vector<ll> > dp(n+1, vector<ll>(k+1, -INF));
  dp[0][0] = 0;

  auto f = [](ll x, ll y){ return max(x,y);};
  for (int j = 1; j <= k; ++j) {
    queue_aggression<ll, decltype(f)> que1(f), que2(f);
    rep(i, n) {
      if (i/m+1 > j) continue;
      // for (int l = max(0, i+1-m); l <= i; ++l) { //区間の始点
      //   // chmax(dp[i+1][j], dp[l][j-1] + (s[i+1] - s[l]));
      //   // chmax(dp[i+1][j], dp[l][j-1] - (s[i+1] - s[l]));
      //   chmax(dp[i+1][j], dp[l][j-1] - s[l] + s[i+1]);
      //   chmax(dp[i+1][j], dp[l][j-1] + s[l] - s[i+1]);
      // } 
      que1.push(dp[i][j-1] - s[i]);
      que2.push(dp[i][j-1] + s[i]);
      chmax(dp[i+1][j], que1.fold_all() + s[i+1]);
      chmax(dp[i+1][j], que2.fold_all() - s[i+1]);
      if (que1.size() == m) {
        que1.pop(); que2.pop();
      }
    }
  }
  cout << dp[n][k] << endl;
  return 0;
}
0