結果

問題 No.15 カタログショッピング
ユーザー nenuonnenuon
提出日時 2017-07-15 11:44:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 1,798 bytes
コンパイル時間 1,090 ms
コンパイル使用メモリ 105,648 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-04-16 21:40:30
合計ジャッジ時間 1,719 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ(β)

テストケース

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

ソースコード

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(int k = n; k >= 1; k--) {
    FOR(i,0,sz-1) {
      for(int j = sz-1; j > i; j--) {
        if(ans[j][k] < ans[j-1][k]) {
          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