結果

問題 No.15 カタログショッピング
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-06-11 11:04:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 2,057 bytes
コンパイル時間 1,892 ms
コンパイル使用メモリ 186,200 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 20:52:42
合計ジャッジ時間 2,563 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 13 ms
4,348 KB
testcase_06 AC 15 ms
4,348 KB
testcase_07 AC 16 ms
4,348 KB
testcase_08 AC 17 ms
4,348 KB
testcase_09 AC 17 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main(){cin.tie(0);ios::sync_with_stdio(0);_main();}
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/




int N, S, P[32];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> S;
    rep(i, 0, N) cin >> P[i];

    if (N == 1) {
        printf("1\n");
        return;
    }

    int M = N / 2;
    vector<pair<int, int>> v;
    rep(mask, 0, 1 << M) {
        int sm = 0;
        rep(i, 0, M) if (mask & (1 << i)) sm += P[i];
        v.push_back({ sm, mask });
    }
    sort(v.begin(), v.end());

    vector<vector<int>> buf;

    int MM = N - M;
    rep(mask, 0, 1 << MM) {
        int sm = 0;
        rep(i, 0, MM) if (mask & (1 << i)) sm += P[M + i];
        auto ite = lower_bound(v.begin(), v.end(), make_pair(S - sm, -1));
        while (ite != v.end()) {
            if (ite->first != S - sm) break;

            vector<int> ans;
            rep(i, 0, M) if (ite->second & (1 << i)) ans.push_back(i + 1);
            rep(i, 0, MM) if (mask & (1 << i)) ans.push_back(M + i + 1);
            buf.push_back(ans);
            
            ite++;
        }
    }

    sort(buf.begin(), buf.end());
    for (auto ans : buf) {
        rep(i, 0, ans.size()) {
            if (0 < i) printf(" ");
            printf("%d", ans[i]);
        }
        printf("\n");
    }
}
0