結果

問題 No.1536 仕切り直し
ユーザー rogi52
提出日時 2022-10-22 13:13:00
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 1,317 bytes
コンパイル時間 1,853 ms
コンパイル使用メモリ 209,828 KB
最終ジャッジ日時 2025-02-08 11:08:45
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using ld = long double;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N,M; cin >> N >> M;
    vector<ll> A(N);
    rep(i,N) cin >> A[i];
    vector<vector<ll>> dp(N + 1, vector<ll>(M + 1, -1e18));
    vector<vector<pair<ll,ll>>> prev(N + 1, vector<pair<ll,ll>>(M + 1, {-1, -1}));
    dp[0][0] = 0;
    rep(i,N+1)rep(j,M+1) {
        if(i + 1 <= N) {
            ll x = (j % 2 == 0 ? +A[i] : -A[i]);
            if(dp[i + 1][j] < dp[i][j] + x) {
                dp[i + 1][j] = dp[i][j] + x;
                prev[i + 1][j] = {i, j};
            }
        }
        if(j + 1 <= M) {
            if(dp[i][j + 1] < dp[i][j]) {
                dp[i][j + 1] = dp[i][j];
                prev[i][j + 1] = {i, j};
            }
        }
    }

    int n = N, m = M;
    vector<pair<int,int>> op = {{n, m}};
    while(true) {
        tie(n, m) = prev[n][m];
        if(make_pair(n, m) == make_pair(-1, -1)) break;
        op.push_back({n, m});
    }
    reverse(op.begin(), op.end());
    vector<int> ans;
    rep(i,int(op.size())-1) {
        auto [n0, m0] = op[i];
        auto [n1, m1] = op[i + 1];
        if(m0 < m1) ans.push_back(n0);
    }
    for(int a : ans) cout << a << "\n";
}
0