結果

問題 No.1117 数列分割
ユーザー Plan8Plan8
提出日時 2020-08-02 11:30:04
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 177 ms / 3,000 ms
コード長 2,350 bytes
コンパイル時間 2,562 ms
コンパイル使用メモリ 209,408 KB
実行使用メモリ 77,972 KB
最終ジャッジ日時 2023-09-25 12:14:20
合計ジャッジ時間 5,578 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 14 ms
9,848 KB
testcase_04 AC 13 ms
9,152 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 12 ms
8,048 KB
testcase_09 AC 5 ms
4,800 KB
testcase_10 AC 14 ms
8,088 KB
testcase_11 AC 42 ms
23,008 KB
testcase_12 AC 43 ms
24,620 KB
testcase_13 AC 47 ms
33,076 KB
testcase_14 AC 59 ms
34,424 KB
testcase_15 AC 62 ms
41,792 KB
testcase_16 AC 82 ms
45,920 KB
testcase_17 AC 14 ms
7,656 KB
testcase_18 AC 177 ms
77,940 KB
testcase_19 AC 125 ms
77,972 KB
testcase_20 AC 73 ms
39,192 KB
testcase_21 AC 112 ms
77,764 KB
testcase_22 AC 111 ms
76,724 KB
testcase_23 AC 114 ms
76,636 KB
testcase_24 AC 112 ms
75,556 KB
testcase_25 AC 111 ms
75,504 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 12 ms
5,924 KB
testcase_28 AC 12 ms
6,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<ll,ll> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

void Main(){
    ll N,K,M; cin >> N >> K >> M;
    VL A(N); REP(i,N) cin >> A[i];
    VL S(N+1,0); REP(i,N) S[i+1] += S[i]+A[i];
    ll dp[N+1][K+1]; REP(i,N+1)REP(j,K+1) dp[i][j] = -LINF;
    deque<P> deq1[K+1],deq2[K+1];

    dp[0][0] = 0; deq1[0].emplace_back(0,0); deq2[0].emplace_back(0,0);
    REP(i,N)REP(j,min((ll)i+1,K)){
        while(!deq1[j].empty() && deq1[j].front().first<i+1-M) deq1[j].pop_front();
        while(!deq2[j].empty() && deq2[j].front().first<i+1-M) deq2[j].pop_front();

        if(!deq1[j].empty()) chmax(dp[i+1][j+1], deq1[j].front().second + S[i+1]);
        if(!deq2[j].empty()) chmax(dp[i+1][j+1], deq2[j].front().second - S[i+1]);

        while(!deq1[j+1].empty() && deq1[j+1].back().second<=dp[i+1][j+1]-S[i+1]) deq1[j+1].pop_back();
        while(!deq2[j+1].empty() && deq2[j+1].back().second<=dp[i+1][j+1]+S[i+1]) deq2[j+1].pop_back();
        deq1[j+1].emplace_back(i+1,dp[i+1][j+1]-S[i+1]);
        deq2[j+1].emplace_back(i+1,dp[i+1][j+1]+S[i+1]);
    }

    cout << dp[N][K] << en;
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}
0