結果

問題 No.1872 Dictionary Order
ユーザー shiomusubi496shiomusubi496
提出日時 2021-11-21 16:12:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,014 bytes
コンパイル時間 2,378 ms
コンパイル使用メモリ 207,668 KB
実行使用メモリ 4,476 KB
最終ジャッジ日時 2023-10-13 20:48:15
合計ジャッジ時間 4,985 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
4,348 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 3 ms
4,352 KB
testcase_04 AC 15 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 WA -
testcase_07 AC 5 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 WA -
testcase_10 AC 6 ms
4,348 KB
testcase_11 WA -
testcase_12 AC 4 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 2 ms
4,352 KB
testcase_17 WA -
testcase_18 AC 7 ms
4,352 KB
testcase_19 AC 5 ms
4,352 KB
testcase_20 AC 5 ms
4,348 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 3 ms
4,348 KB
testcase_31 WA -
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

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

    vector<vector<bool>> dp(N + 1, vector<bool>(M + 1, false));
    dp[N][0] = true;
    for (int i = N - 1; i >= 0; --i) {
        for (int j = 0; j <= M; ++j) dp[i][j] = dp[i + 1][j];
        for (int j = A[i]; j <= M; ++j) dp[i][j] = dp[i][j] || dp[i + 1][j - A[i]];
    }

    if (!dp[0][M]) {
        puts("-1");
        return 0;
    }

    vector<int> P(N);
    for (int i = 0; i < N; ++i) {
        int a; cin >> a;
        P[a - 1] = i;
    }

    int sum = M;
    vector<int> ans{-1};
    for (int i = 0; i < N; ++i) {
        if (ans.back() >= P[i]) continue;
        if (sum >= A[P[i]] && dp[P[i]][sum] && dp[P[i] + 1][sum - A[P[i]]]) {
            ans.push_back(P[i]);
            sum -= A[P[i]];
        }
    }

    cout << ans.size() - 1 << endl;
    for (int i = 1; i < ans.size(); ++i) cout << ans[i] + 1 << " \n"[i == ans.size() - 1];
}
0