結果

問題 No.1872 Dictionary Order
ユーザー 👑 colognecologne
提出日時 2022-03-11 21:50:55
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 1,158 bytes
コンパイル時間 3,367 ms
コンパイル使用メモリ 251,264 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 12:35:27
合計ジャッジ時間 6,929 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 9 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 8 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 8 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 20 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 6 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 4 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 205 ms
4,348 KB
testcase_23 AC 214 ms
4,348 KB
testcase_24 AC 220 ms
4,348 KB
testcase_25 AC 269 ms
4,348 KB
testcase_26 AC 242 ms
4,348 KB
testcase_27 AC 255 ms
4,348 KB
testcase_28 AC 218 ms
4,348 KB
testcase_29 AC 281 ms
4,348 KB
testcase_30 AC 3 ms
4,348 KB
testcase_31 AC 236 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;

using Line = bitset<2001>;

int main()
{
    int N, M;
    cin >> N >> M;
    vector<int> A(N);
    for (int &a : A)
        cin >> a;
    vector<int> P(N);
    for (int &p : P)
        cin >> p;
    vector<int> Pinv(N);
    for (int i = 0; i < N; ++i)
        Pinv[P[i] - 1] = i;
    int last_idx = -1;
    vector<int> ans;
    for (int i = 0; i < N; ++i)
    {

        int p_idx = Pinv[i];
        // cout << p_idx << " " << i << " " << M << " " << A[p_idx] << endl;
        if (p_idx <= last_idx)
            continue;
        Line z = 0; z[0] = 1;
        for (int j = p_idx + 1; j < N; ++j)
            z |= z << A[j];
        // cout << z[1] << " " << z[0] << endl;
        if (M >= A[p_idx] && z[M - A[p_idx]])
        {
            ans.push_back(p_idx + 1);
            last_idx = p_idx;
            M -= A[p_idx];
            i = -1;
        }
    }
    if (M != 0)
    {
        cout << -1 << endl;
        assert(ans.empty());
    }
    else
    {
        assert(!ans.empty());
        cout << ans.size() << endl;
        for (int x : ans)
            cout << x << " ";
        cout << endl;
    }
}
0