結果

問題 No.15 カタログショッピング
ユーザー y_mazuny_mazun
提出日時 2015-04-24 01:02:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 35 ms / 5,000 ms
コード長 957 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 83,124 KB
実行使用メモリ 6,644 KB
最終ジャッジ日時 2023-09-18 08:51:05
合計ジャッジ時間 2,150 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 26 ms
6,624 KB
testcase_06 AC 32 ms
6,636 KB
testcase_07 AC 35 ms
6,644 KB
testcase_08 AC 34 ms
6,616 KB
testcase_09 AC 35 ms
6,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }

#include <map>
#include <queue>
#include <set>

using namespace std;

int main(){
  const int n = getInt();
  const int s = getInt();
  vector<int> p(n);
  REP(i,n) p[i] = getInt();

  map<int, vector<int> > m;

  REP(i,1<<(n/2)){
    int s = 0;
    REP(j,n/2) if(i & (1 << j)) s += p[j];
    m[s].push_back(i);
  }

  vector<vector<int> > ans;
  REP(i,1<<(n/2+n%2)){
    int t = 0;
    REP(j,n/2+n%2) if(i & (1 << j)) t += p[n/2 + j];

    if(m.count(s - t)){
      REP(j,m[s-t].size()){
        vector<int> tmp;
        const int f = m[s-t][j] | (i << (n/2));
        REP(k,n) if(f & (1 << k))
          tmp.push_back(k + 1);
        ans.push_back(tmp);
      }
    }
  }

  sort(ans.begin(), ans.end());
  REP(i,ans.size()){
    REP(j,ans[i].size()) printf("%d ", ans[i][j]);
    puts("");
  }

  return 0;
}
0