結果

問題 No.15 カタログショッピング
ユーザー 0w10w1
提出日時 2017-01-02 14:17:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,941 bytes
コンパイル時間 2,001 ms
コンパイル使用メモリ 183,704 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-22 07:43:23
合計ジャッジ時間 2,835 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef pair< int, int > pii;
typedef vector< pii > vp;
typedef vector< double > vd;
typedef vector< vd > vvd;
typedef vector< string > vs;

template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
  if( x > v ){
    x = v;
    return 1;
  }
  return 0;
}

template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
  if( x < v ){
    x = v;
    return 1;
  }
  return 0;
}

const int INF = 0x3f3f3f3f;

int N, S;
vi P;

void init(){
  cin >> N >> S;
  P = vi( N );
  for( int i = 0; i < N; ++i ){
    cin >> P[ i ];
  }
}

vvi ans;

void preprocess(){
  int n0 = N / 2, n1 = N - N / 2;
  vp v_ids;
  for( int s = 0; s < 1 << n1; ++s ){
    int v = 0;
    for( int i = 0; i < n1; ++i ){
      if( s & 1 << i ){
        v += P[ n0 + i ];
      }
    }
    v_ids.emplace_back( v, s );
  }
  sort( v_ids.begin(), v_ids.end() );
  for( int s = 0; s < 1 << n0; ++s ){
    int v = 0;
    for( int i = 0; i < n0; ++i ){
      if( s & 1 << i ){
        v += P[ i ];
      }
    }
    auto it = lower_bound( v_ids.begin(), v_ids.end(), make_pair( S - v, 0 ) );
    for( auto a = it; a != v_ids.end(); ++a ){
      if( a->first != S - v ){
        break;
      }
      vi tmp;
      for( int i = 0; i < n0; ++i ){
        if( s & 1 << i ){
          tmp.emplace_back( i + 1 );
        }
      }
      for( int i = 0; i < n1; ++i ){
        if( a->second & 1 << i ){
          tmp.emplace_back( n0 + i + 1 );
        }
      }
      ans.emplace_back( tmp );
    }
  }
  sort( ans.begin(), ans.end() );
}

void solve(){
  for( int i = 0; i < ans.size(); ++i ){
    for( int j = 0; j < ans[ i ].size(); ++j ){
      cout << ans[ i ][ j ] << " \n"[ j + 1 == ans[ i ].size() ];
    }
  }
}

signed main(){
  ios::sync_with_stdio( 0 );
  init();
  preprocess();
  solve();
  return 0;
}
0