結果

問題 No.15 カタログショッピング
ユーザー yun_appyun_app
提出日時 2016-05-13 22:25:05
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,092 bytes
コンパイル時間 2,351 ms
コンパイル使用メモリ 77,964 KB
実行使用メモリ 39,904 KB
最終ジャッジ日時 2024-04-15 16:33:36
合計ジャッジ時間 9,219 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
37,024 KB
testcase_01 AC 52 ms
36,916 KB
testcase_02 AC 57 ms
37,236 KB
testcase_03 AC 88 ms
39,904 KB
testcase_04 AC 52 ms
36,668 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{

    static int n;
    static int target;
    static int SUM;
    static HashMap<Integer,Integer> remMap = new HashMap<Integer,Integer>();
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] lines = br.readLine().split(" ");
        n = Integer.parseInt(lines[0]);
        target = Integer.parseInt(lines[1]);
        ArrayList<Integer> prices = new ArrayList<Integer>();
        for(int i=0;i<n;i++){
            int p = Integer.parseInt(br.readLine());
            prices.add(p);
            remMap.put(i,SUM);
            SUM += p;
        }
        //Collections.reverse(prices);
        check(prices,new ArrayList<Integer>(),0,0);
        for(String a:ans){
            System.out.println(a);
        }
    }
    static ArrayList<String> ans = new ArrayList<String>();
    public static int check(ArrayList<Integer> prices,ArrayList<Integer> items,int sum,int idx){
        if(sum == target){
            ans.add(toString(items));
            return 1;
        }
        if(ans.size()>50 || idx >= n || target>SUM-remMap.get(idx)+sum){
            //System.out.println(SUM + " " + remMap.get(idx) + " " + sum);
            return 0;
        }
        int price = prices.get(idx);
        ArrayList<Integer> cp_items = new ArrayList<Integer>(items);
        cp_items.add(idx+1);
        int s = 0;
        s += check(prices,cp_items,sum+price,idx+1);
        s += check(prices,new ArrayList<Integer>(items),sum,idx+1);
        return s;
    }
    
    public static String toString(ArrayList<Integer> items){
        StringBuilder sb = new StringBuilder();
        for(int i:items){
            if(sb.length() > 0){
                sb.append(" ");
            }
            sb.append(i);
        }
        return sb.toString();
    }
}
0