結果

問題 No.1872 Dictionary Order
ユーザー ぷらぷら
提出日時 2021-11-21 18:11:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 1,221 bytes
コンパイル時間 2,373 ms
コンパイル使用メモリ 209,560 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 12:34:20
合計ジャッジ時間 3,621 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

bitset<2001> dp[2001];

int main() {
    int N,M;
    cin >> N >> M;
    vector<int>A(N),P(N),Q(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }
    for(int i = 0; i < N; i++) {
        cin >> P[i];
        Q[P[i]-1] = i;
    }
    dp[N][0] = 1;
    for(int i = N-1; i >= 0; i--) {
        dp[i] = dp[i+1]|(dp[i+1] << A[i]);
    }
    if(!dp[0][M]) {
        cout << -1 << endl;
        return 0;
    }
    vector<int>ans,tmp(N);
    ans.push_back(-1);
    for(int i = 0; i < N; i++) {
        tmp[i] = Q[i];
    }
    int now = M;
    while (now != 0) {
        vector<int>nxt;
        bool flag = false;
        for(int i = 0; i < tmp.size(); i++) {
            if(ans.back() < tmp[i] && now >= A[tmp[i]]) {
                nxt.push_back(tmp[i]);
                if(dp[tmp[i]+1][now-A[tmp[i]]] && !flag) {
                    ans.push_back(tmp[i]);
                    now -= A[tmp[i]];
                    flag = true;
                }
            }
        }
        tmp = nxt;
    }
    ans.erase(ans.begin());
    cout << ans.size() << endl;
    for(int i = 0; i < ans.size(); i++) {
        cout << ans[i]+1 << ((i+1 == ans.size())?"\n":" ");
    }
}
0