結果

問題 No.1117 数列分割
ユーザー pentapenta
提出日時 2020-07-25 17:51:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 802 ms / 3,000 ms
コード長 3,324 bytes
コンパイル時間 2,225 ms
コンパイル使用メモリ 207,476 KB
最終ジャッジ日時 2025-01-12 05:36:02
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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