結果

問題 No.1536 仕切り直し
ユーザー rogi52rogi52
提出日時 2022-10-22 13:13:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,317 bytes
コンパイル時間 2,038 ms
コンパイル使用メモリ 214,740 KB
実行使用メモリ 70,656 KB
最終ジャッジ日時 2023-09-14 08:02:35
合計ジャッジ時間 3,871 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 4 ms
5,692 KB
testcase_04 AC 19 ms
24,052 KB
testcase_05 AC 39 ms
48,168 KB
testcase_06 AC 2 ms
4,500 KB
testcase_07 AC 3 ms
5,440 KB
testcase_08 AC 16 ms
20,188 KB
testcase_09 AC 22 ms
26,272 KB
testcase_10 AC 61 ms
70,656 KB
testcase_11 AC 11 ms
13,564 KB
testcase_12 AC 43 ms
49,524 KB
権限があれば一括ダウンロードができます

ソースコード

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