結果

問題 No.1117 数列分割
ユーザー shinchanshinchan
提出日時 2020-06-02 00:42:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,614 bytes
コンパイル時間 2,326 ms
コンパイル使用メモリ 211,552 KB
実行使用メモリ 77,924 KB
最終ジャッジ日時 2023-08-20 03:40:19
合計ジャッジ時間 6,023 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=(1LL<<60);
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;

void chmax(ll &a, ll b){a = max(a, b);}
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    ll n, k, m;
    cin >> n >> k >> m;
    vector<ll> a(n), sum(n+1,0);
    for(int i=0;i<n;i++){
    	cin >> a[i];
    	sum[i+1]=sum[i]+a[i];
    }
    vector<deque<pair<int, ll> > > dqp(k+1), dqm(k+1);
    dqp[0].push_back({0, a[0]});
    dqm[0].push_back({0, -a[0]});
    vector<vector<ll> > dp(n+1, vector<ll> (k+1, 0));
    for(int i=0;i<n;i++){
    	for(int j=0;j<=k;j++) dp[i+1][j] = 0;
    	for(int j=k-1;j>=0;j--){
    		while(!dqp[j].empty() && dqp[j].front().first < i-m) dqp[j].pop_front();
    		while(!dqm[j].empty() && dqm[j].front().first < i-m) dqm[j].pop_front();
    		if(!dqp[j].empty()) chmax(dp[i+1][j+1], dqp[j].front().second + abs(sum[i+1] - sum[dqm[j].front().first]));
    		if(!dqm[j].empty()) chmax(dp[i+1][j+1], dqm[j].front().second + abs(sum[i+1] - sum[dqm[j].front().first]));
    		if (j + 1 < k) {
                while (!dqp[j + 1].empty() && dqp[j + 1].back().second <= dp[i + 1][j + 1]) dqp[j + 1].pop_back();
                while (!dqm[j + 1].empty() && dqm[j + 1].back().second <= dp[i + 1][j + 1]) dqm[j + 1].pop_back();
                dqp[j + 1].emplace_back(i, dp[i + 1][j + 1]);
                dqm[j + 1].emplace_back(i, dp[i + 1][j + 1]);
            }
    	}
    }
    cout << dp[n][k] << endl;
    return 0;
}
0