結果

問題 No.15 カタログショッピング
ユーザー 0w1
提出日時 2017-01-02 14:17:14
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,941 bytes
コンパイル時間 1,881 ms
コンパイル使用メモリ 186,796 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-16 03:17:58
合計ジャッジ時間 2,447 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

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