結果

問題 No.15 カタログショッピング
ユーザー どららどらら
提出日時 2015-06-03 00:45:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 1,747 bytes
コンパイル時間 1,069 ms
コンパイル使用メモリ 99,100 KB
実行使用メモリ 7,168 KB
最終ジャッジ日時 2023-09-20 18:55:48
合計ジャッジ時間 1,825 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;

ll N, G, p;
vector<ll> P;


bool operator<(const vector<int>& a, const vector<int>& b){
  int i = 0;
  while(i<a.size() && i<b.size()){
    if(a[i] < b[i]) return true;
    if(a[i] > b[i]) return false;
  }
  return a.size() < b.size();
}

map<ll,vector<int> > memo;


int main(){
  ios::sync_with_stdio(false);
  cin >> N >> G;
  rep(i,N){
    cin >> p;
    P.push_back(p);
  }

  if(N==1){
    if(G == P[0]){ cout << 0 << endl; }
    return 0;
  }
  
  int N1 = P.size()/2;
  int N2 = N-N1;
  for(int S=0; S<(1<<N1); S++){
    ll sum = 0;
    rep(i,N1){
      if((S>>i)&1) sum += P[i];
    }
    memo[sum].push_back(S);
  }

  vector< vector<int> > ret;
  
  for(int S=0; S<(1<<N2); S++){
    ll sum = 0;
    rep(i,N2){
      if((S>>i)&1) sum += P[i+N1];
    }
    if(memo.count(G-sum) > 0){
      vector<int> v = memo[G-sum];
      rep(i,v.size()){
        int T = v[i];
        vector<int> w;
        rep(j,N1){
          if((T>>j)&1) w.push_back(j+1);
        }
        rep(j,N2){
          if((S>>j)&1) w.push_back(N1+j+1);
        }
        ret.push_back(w);
      }

    }
  }
  
  sort(ALLOF(ret));
  
  rep(i,ret.size()){
    rep(j,ret[i].size()){
      if(j!=0) cout << " ";
      cout << ret[i][j];
    }
    cout << endl;
  }
  return 0;
}
0