結果

問題 No.15 カタログショッピング
ユーザー nenuonnenuon
提出日時 2017-07-15 11:23:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,911 bytes
コンパイル時間 1,001 ms
コンパイル使用メモリ 105,300 KB
実行使用メモリ 7,424 KB
最終ジャッジ日時 2024-04-16 21:39:33
合計ジャッジ時間 1,612 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;

int main() {
  int n; cin>>n;
  ll s; cin>>s;
  ll p[n];
  FOR(i,0,n) {
    scanf("%lld", p+i);
  }
  map<ll, vector<int> > S; // 合計金額, 集合
  // 半分全列挙
  int n2 = n / 2;
  FOR(i,0,1<<n2) {
    ll sum = 0;
    FOR(j,0,n2) {
      if((i>>j)&1) {
        sum += p[j];
      }
    }
    S[sum].push_back(i);
  }
  // 残り半分で作れるやつ
  vector<vector<int> > ans(50, vector<int>(1, 0));
  int cnt = 0;
  FOR(i,0,1<<(n-n2)) {
    int sum = 0;
    FOR(j,0,n-n2) {
      if((i>>j)&1) {
        sum += p[j+n2];
      }
    }
    if(S.count(s-sum)>0) {
      for(int x : S[s-sum]) {
        FOR(j,0,n2) {
          if((x>>j)&1) {
            //cout << j + 1 << " ";
            ans[cnt].push_back(j+1);
          }
        }
        FOR(j,0,n-n2) {
          if((i>>j)&1) {
            //cout << j + n2 + 1 << " ";
            ans[cnt].push_back(j+n2+1);
          }
        }
        //cout << endl;
        while(ans[cnt].size()<n+1) {
          ans[cnt].push_back(0);
        }
        cnt++;
      }
    }
  }
  int sz = cnt;
  FOR(i,0,sz) {
    FOR(j,0,ans[i].size()) {
      cout<<ans[i][j];
    }
    cout<<endl;
  }
  // 答えを昇順に並び替える
  FOR(k,0,n+1) {
    FOR(i,0,sz-1) {
      for(int j = sz-1; j > i; j--) {
        if(ans[j][k] < ans[j-1][k] && ans[j][k-1] == ans[j-1][k-1] ) {
          FOR(w,0,n+1) {
            swap(ans[j][w], ans[j-1][w]);
          }
        }
      }
    }
  }
  FOR(i,0,sz) {
    FOR(j,0,n+1) {
      if(ans[i][j]!=0) cout << ans[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}
0