結果

問題 No.15 カタログショッピング
ユーザー goodbatongoodbaton
提出日時 2015-07-28 20:26:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 2,056 bytes
コンパイル時間 928 ms
コンパイル使用メモリ 90,196 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-24 12:28:48
合計ジャッジ時間 2,194 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstring>

typedef long long ll;
using namespace std;

#define mod 1000003
#define INF 1000000000
#define LLINF 2000000000000000000LL

#define SIZE 100
#define EPS 1e-12

vector<pair<int,int> > half;
vector<vector<int> > ANS;
int cho[16];
int N,S,p[31];

void dfs1(int n,int s=0,int ch=0,int sump=0){
    
    if(s==n){
        half.push_back({sump,ch});
        return;
    }
    
    dfs1(n,s+1,ch+(1<<s),sump+p[s]);
    
    dfs1(n,s+1,ch,sump);
    
}

void dfs2(int n,int s,int ch=0,int sump=0){
    
    if(s==n){
        
        if(sump>S) return;
        
        int l=0,r=(int)half.size()-1,mid;
        
        while(l<r){
            mid = (l+r)/2;
            
            if(half[mid].first>=S-sump){
                r = mid;
            }else{
                l = mid+1;
            }
            
        }
        
        for(int i=l;;i++){
            if(half[i].first+sump==S){
                
                int CH = ch + half[i].second;
                vector<int> ans;
                
                for(int j=0;j<n;j++){
                    if(CH%2){
                        ans.push_back(j+1);
                    }
                    CH/=2;
                }
                
                ANS.push_back(ans);
                
            }else{
                return;
            }
        }
        
        
    }
    
    
    dfs2(n,s+1,ch+(1<<s),sump+p[s]);
    dfs2(n,s+1,ch,sump);
}

int main(){
    
    scanf("%d%d",&N,&S);
    
    for(int i=0;i<N;i++)
        scanf("%d",&p[i]);
    
    
    dfs1((N+1)/2);
    
    sort(half.begin(),half.end());
    
    dfs2(N,(N+1)/2);
    
    sort(ANS.begin(),ANS.end());
    
    for(int i=0;i<ANS.size();i++){
        
        for(int j=0;j<ANS[i].size();j++){
            printf(j<ANS[i].size()-1 ? "%d ":"%d\n",ANS[i][j]);
        }
    }
    
    return 0;
}
0