結果
| 問題 | No.15 カタログショッピング | 
| コンテスト | |
| ユーザー |  goodbaton | 
| 提出日時 | 2015-07-28 20:26:42 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 15 ms / 5,000 ms | 
| コード長 | 2,056 bytes | 
| コンパイル時間 | 1,300 ms | 
| コンパイル使用メモリ | 90,516 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-17 13:23:50 | 
| 合計ジャッジ時間 | 2,057 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 10 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:91:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   91 |     scanf("%d%d",&N,&S);
      |     ~~~~~^~~~~~~~~~~~~~
main.cpp:94:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   94 |         scanf("%d",&p[i]);
      |         ~~~~~^~~~~~~~~~~~
            
            ソースコード
#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;
}
            
            
            
        