結果

問題 No.1117 数列分割
ユーザー chineristACchineristAC
提出日時 2020-07-18 00:08:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,991 ms / 3,000 ms
コード長 3,534 bytes
コンパイル時間 1,249 ms
コンパイル使用メモリ 115,660 KB
実行使用メモリ 74,040 KB
最終ジャッジ日時 2023-08-20 05:17:16
合計ジャッジ時間 24,366 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 145 ms
8,780 KB
testcase_04 AC 132 ms
8,268 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 153 ms
7,508 KB
testcase_09 AC 46 ms
4,532 KB
testcase_10 AC 159 ms
7,860 KB
testcase_11 AC 728 ms
21,772 KB
testcase_12 AC 803 ms
23,264 KB
testcase_13 AC 681 ms
30,896 KB
testcase_14 AC 1,079 ms
32,256 KB
testcase_15 AC 1,152 ms
39,192 KB
testcase_16 AC 1,657 ms
43,836 KB
testcase_17 AC 196 ms
7,552 KB
testcase_18 AC 1,564 ms
73,908 KB
testcase_19 AC 1,748 ms
73,884 KB
testcase_20 AC 1,436 ms
37,444 KB
testcase_21 AC 1,988 ms
74,040 KB
testcase_22 AC 1,982 ms
72,952 KB
testcase_23 AC 1,991 ms
72,832 KB
testcase_24 AC 1,947 ms
71,844 KB
testcase_25 AC 1,948 ms
71,760 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 100 ms
5,976 KB
testcase_28 AC 98 ms
5,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<random>
#include<stdio.h>
using namespace std;

typedef long long ll;
typedef pair<ll,ll> P;

long long Mod(long long val, long long m) {
  long long res = val % m;
  if (res < 0) res += m;
  return res;
}

#define INF 1e19;

struct SegmentTree {
   ll n;  // 最下段のノード数
   vector<ll> node;
   ll SIJK;  // 最弱なやつ(INFとか-1とか)
 
  public:
   SegmentTree(ll v) {
     SIJK = -(1ll << 55) ;  // INFは最弱なので
 
     ll sz = v;
     n = 1;
     while (n < sz) n *= 2;  // n…最下段の横幅
     node.resize(2 * n - 1, SIJK);
 
     // 最下段以外を更新していく
     for (ll i = n - 2; i >= 0; i--) {
       node[i] = compare(node[i * 2 + 1], node[i * 2 + 2]);
     }
   }
 
   // 結合法則を満たすやつならなんでもいいよー。aかbを返す。
   ll compare(ll a, ll b) {
     return max(a, b);
   }
 
   // i番目の要素をvalに変更する
   void update(ll i, ll val) {
     // まず最下段(2n-1)を変更する
     i += n - 1;
     node[i] = val;
 
     // 上に行きながら更新していく
     while (i > 0) {
       i = (i - 1) / 2;  // 親へ
       node[i] = compare(node[2 * i + 1], node[2 * i + 2]);
     }
   }
 
   // [a,b) 中の結果を返す。[l,r)は対称区間の左端と右端。
   ll find(ll a,ll b, ll now = 0, ll l = 0, ll r = -1) {
     // 初期化
     if (r < 0) r = n;
 
     // 俺は関係ないとき -> 答えの邪魔にならない値を返す
     if (r <= a || b <= l) return SIJK;
 
     // 要求区間の中にノードがすっぽり入ってる → 計算候補として返す
     if (a <= l && r <= b) return node[now];
 
     // ノードの一部分だけ要求区間に入ってる → 子を再帰的に探索する
     ll vl = find(a, b, 2 * now + 1, l, (l + r) / 2);  // 子(左)
     ll vr = find(a, b, 2 * now + 2, (l + r) / 2, r);  // 子(右)
     return compare(vl, vr);
   }
 };


int main(){
    ll N,K,M;
    cin>>N>>K>>M;
    vector<ll> A(N);
    for (int i=0;i<N;i++){
        cin>>A[i];
    }

    vector<ll> imos(N);
    for (int i=0;i<N;i++){
        imos[i]=A[i];
    }
    for (int i=1;i<N;i++){
        imos[i]+=imos[i-1];
    }

    vector<P> data(N);
    for (int i=0;i<N;i++){
        data[i]={imos[i],i};
    }
    sort(data.begin(),data.end());

    ll dp[K][N];
    for (int i=0;i<K;i++){
        for (int j=0;j<N;j++){
            dp[i][j]=-10000000000000000;
        }
    }

    for (int i=0;i<M;i++){
        dp[0][i]=abs(imos[i]);
    }

    for (ll i=1;i<K;i++){
        vector<ll> Low(N,0) ;
        vector<ll> High(N,0);
        SegmentTree lowseg(N);
        SegmentTree highseg(N);
        for (ll j=0;j<N;j++){
            ll val=data[j].first;
            ll id=data[j].second;
            ll L=max(id-M,i-1);
            ll R=id;
            if (R>L){
                Low[id]=lowseg.find(L,R)+imos[id];
            }
            lowseg.update(id,dp[i-1][id]-imos[id]);
        }
        for (ll j=N-1;j>-1;j--){
            ll val=data[j].first;
            ll id=data[j].second;
            ll L=max(id-M,i-1);
            ll R=id;
            if (R>L){
                High[id]=highseg.find(L,R)-imos[id];
            }
            highseg.update(id,dp[i-1][id]+imos[id]);
        }
        for (ll j=i;j<N;j++){
            dp[i][j]=max(Low[j],High[j]);
        }
    }
    cout<<dp[K-1][N-1]<<endl;
}
0