結果

問題 No.1872 Dictionary Order
ユーザー SSRSSSRS
提出日時 2022-03-11 21:43:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,193 bytes
コンパイル時間 2,261 ms
コンパイル使用メモリ 207,892 KB
実行使用メモリ 15,160 KB
最終ジャッジ日時 2023-10-14 10:48:44
合計ジャッジ時間 8,731 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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<int> P(N);
  for (int i = 0; i < N; i++){
    cin >> P[i];
    P[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++){
      if (dp[i + 1][j]){
        dp[i][j] = true;
        if (j + A[i] <= M){
          dp[i][j + A[i]] = true;
        }
      }
    }
  }
  if (!dp[0][M]){
    cout << -1 << endl;
  } else {
    vector<int> Q(N);
    for (int i = 0; i < N; i++){
      Q[P[i]] = i;
    }
    vector<int> ans;
    int sum = 0;
    int L = 0;
    while (sum < M){
      for (int i = 0; i < N; i++){
        if (Q[i] > L && sum + A[Q[i]] <= M){
          if (dp[Q[i] + 1][M - A[Q[i]] - sum]){
            ans.push_back(Q[i]);
            L = Q[i];
            sum += A[Q[i]];
            break;
          }
        }
      }
    }
    int K = ans.size();
    cout << K << endl;
    for (int i = 0; i < K; i++){
      cout << ans[i] + 1;
      if (i < K - 1){
        cout << ' ';
      }
    }
    cout << endl;
  }
}
0