結果

問題 No.15 カタログショッピング
ユーザー uafr_csuafr_cs
提出日時 2015-10-12 01:37:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,462 ms / 5,000 ms
コード長 2,830 bytes
コンパイル時間 2,445 ms
コンパイル使用メモリ 76,628 KB
実行使用メモリ 62,836 KB
最終ジャッジ日時 2023-09-28 12:31:12
合計ジャッジ時間 8,122 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,556 KB
testcase_01 AC 127 ms
55,800 KB
testcase_02 AC 132 ms
56,120 KB
testcase_03 AC 132 ms
55,640 KB
testcase_04 AC 128 ms
55,872 KB
testcase_05 AC 420 ms
62,016 KB
testcase_06 AC 557 ms
62,836 KB
testcase_07 AC 900 ms
62,440 KB
testcase_08 AC 1,462 ms
62,540 KB
testcase_09 AC 937 ms
61,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
	
	public static class Item implements Comparable<Item> {
		long cost;
		int index;
		
		public Item(long cost, int index) {
			super();
			this.cost = cost;
			this.index = index;
		}

		@Override
		public int compareTo(Item o) {
			if(Long.compare(o.cost, this.cost) != 0){
				return Long.compare(o.cost, this.cost);
			}else{
				return Integer.compare(this.index, o.index);
			}
		}
	}
	
	public static class Seq implements Comparable<Seq> {
		List<Integer> seq;
		
		public Seq(List<Integer> seq){
			this.seq = new ArrayList<Integer>(new TreeSet<Integer>(seq));
			//System.out.println(seq);
		}
		
		@Override
		public int compareTo(Seq arg0){
			final int min_size = Math.min(this.seq.size(), arg0.seq.size());
			final int max_size = Math.max(this.seq.size(), arg0.seq.size());
			
			for(int i = 0; i < min_size; i++){
				if(Integer.compare(this.seq.get(i), arg0.seq.get(i)) != 0){
					return Integer.compare(this.seq.get(i), arg0.seq.get(i));
				}
			}
			
			if(min_size == max_size){
				return 0;
			}else if(min_size == this.seq.size()){
				return -1;
			}else{
				return 1;
			}
		}
		
		@Override
		public String toString(){
			StringBuilder sb = new StringBuilder();
			boolean first = true;
			
			
			for(final int item : this.seq){
				if(first){
					first = false;
				}else{
					sb.append(" ");
				}
				
				sb.append(item + 1);
			}
			
			return sb.toString();
		}
		
	}
	
	public static void dfs(final long S, final int N, final int deep, Item[] items, long[] maximum, LinkedList<Integer> stack, TreeSet<Seq> answers){
		if(S == 0){
			answers.add(new Seq(stack));
			
			return;
		}else if(deep >= N){
			return;
		}else if(maximum[deep] < S){
			return;
		}
		
		if(S >= items[deep].cost){
			stack.addLast(items[deep].index);
			dfs(S - items[deep].cost, N, deep + 1, items, maximum, stack, answers);
			stack.removeLast();
		}
		dfs(S, N, deep + 1, items, maximum, stack, answers);
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final long S = sc.nextLong();
		
		Item[] items = new Item[N];
		for(int i = 0; i < N; i++){
			items[i] = new Item(sc.nextLong(), i);
		}
		Arrays.sort(items);
		
		long[] maximum = new long[N];
		for(int i = N - 1; i >= 0; i--){
			maximum[i] = items[i].cost + (i == N - 1 ? 0 : maximum[i + 1]);
		}
		//System.out.println(Arrays.toString(maximum) + " " + items[0].cost);
		
		TreeSet<Seq> answers = new TreeSet<Seq>();
		dfs(S, N, 0, items, maximum, new LinkedList<Integer>(), answers);
	
		for(final Seq ans : answers){
			System.out.println(ans);
		}
	}

}
0