結果
問題 | No.15 カタログショッピング |
ユーザー |
|
提出日時 | 2014-11-12 03:35:37 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 374 ms / 5,000 ms |
コード長 | 2,060 bytes |
コンパイル時間 | 2,681 ms |
コンパイル使用メモリ | 82,808 KB |
実行使用メモリ | 61,468 KB |
最終ジャッジ日時 | 2024-12-31 09:58:01 |
合計ジャッジ時間 | 6,206 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 |
ソースコード
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; /** * * @author scache * */ public class Main { public static void main(String[] args) { Main p = new Main(); } public Main() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int s = sc.nextInt(); int[] p = new int[n]; for(int i=0;i<n;i++) p[i] = sc.nextInt(); solve(s, p); } ArrayList<ShoppingInfomation> list; public void solve(int s, int[] p) { list = new ArrayList<ShoppingInfomation>(); suf(s, p, p.length/2, 0, 0); Collections.sort(list); pre(s, p, 0, 0, 0); } private void pre(int s, int[] p, int n, int sum, int used){ if(sum>s) return; if(n==p.length/2){ int index = Collections.binarySearch(list, new ShoppingInfomation(s-sum, 0)); if(index<0) return ; for(int i=index;i>=0;i--){ if(list.get(i).price+sum==s) printResult(p, used+list.get(i).used); else break; } for(int i=index+1;i<list.size();i++){ if(list.get(i).price+sum==s) printResult(p, used+list.get(i).used); else break; } return; } pre(s, p, n+1, sum+p[n], used|(1<<(p.length-n-1))); pre(s, p, n+1, sum, used); } private void suf(int s, int[] p, int n, int sum, int used){ if(sum>s) return; if(n==p.length){ list.add(new ShoppingInfomation(sum, used)); return; } suf(s, p, n+1, sum+p[n], used|(1<<(p.length-n-1))); suf(s, p, n+1, sum, used); } private void printResult(int[] p, int used){ for(int i=p.length-1;0<=i;i--){ if(((used>>i)&1)==1){ System.out.print(p.length-i +" "); } } System.out.println(""); } private class ShoppingInfomation implements Comparable<ShoppingInfomation>{ int price; int used; public ShoppingInfomation(int price, int used){ this.price = price; this.used = used; } @Override public int compareTo(ShoppingInfomation o) { return price-o.price; } } }