結果

問題 No.4 おもりと天秤
ユーザー maruyuki95maruyuki95
提出日時 2020-07-07 20:50:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 194 ms / 5,000 ms
コード長 1,931 bytes
コンパイル時間 2,358 ms
コンパイル使用メモリ 82,404 KB
実行使用メモリ 57,372 KB
最終ジャッジ日時 2024-04-09 19:19:19
合計ジャッジ時間 6,563 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
54,000 KB
testcase_01 AC 108 ms
53,144 KB
testcase_02 AC 121 ms
54,196 KB
testcase_03 AC 123 ms
54,008 KB
testcase_04 AC 122 ms
53,956 KB
testcase_05 AC 183 ms
56,996 KB
testcase_06 AC 104 ms
52,736 KB
testcase_07 AC 189 ms
56,972 KB
testcase_08 AC 129 ms
54,440 KB
testcase_09 AC 126 ms
54,252 KB
testcase_10 AC 186 ms
57,372 KB
testcase_11 AC 117 ms
53,764 KB
testcase_12 AC 117 ms
53,860 KB
testcase_13 AC 117 ms
54,052 KB
testcase_14 AC 108 ms
52,724 KB
testcase_15 AC 101 ms
52,604 KB
testcase_16 AC 106 ms
53,124 KB
testcase_17 AC 123 ms
54,004 KB
testcase_18 AC 194 ms
57,304 KB
testcase_19 AC 182 ms
56,884 KB
testcase_20 AC 173 ms
56,756 KB
testcase_21 AC 170 ms
56,064 KB
testcase_22 AC 184 ms
57,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static void main(String[] args) {
		new Main().execute();
	}

	private void execute() {
		int[] weights = loadInputValue();
		outputResult(canDevideInHalf(weights));
	}

	boolean canDevideInHalf(int[] weights) {
		int totalWeight = total(weights);
		if (totalWeight % 2 != 0) {
			return false;
		}

		int goalWeight = totalWeight / 2;
		int remainingWeight = totalWeight;
		List<Integer> sortedWeights = convertToSortedList(weights);
		Set<Integer> availavleWeights = new HashSet<>();
		availavleWeights.add(0);
		for (Integer weight : sortedWeights) {
			remainingWeight -= weight;
			
			Set<Integer> additionalWeights = new HashSet<>(availavleWeights);
			for (Integer availavleWeight : availavleWeights) {
				int e = availavleWeight + weight;
				if (e == goalWeight) {
					return true;
				} else if (e > goalWeight) {
					continue;
				} else if (e + remainingWeight < goalWeight) {
					continue;
				}
				additionalWeights.add(e); 
			}
			availavleWeights = additionalWeights;
		}
		
		return false;
	}

	private List<Integer> convertToSortedList(int[] array) {
		List<Integer> ret = new ArrayList<>();
		for (int i : array) {
			ret.add(i);
		}
		Collections.sort(ret);
		return ret ;
	}
	
	private int total(int[] values) {
		int totalValue = 0;
		for (int value : values) {
			totalValue += value;
		}
		return totalValue;
	}

	@SuppressWarnings("resource")
	private int[] loadInputValue() {
		Scanner sc = new Scanner(System.in);
		int[] ret = new int[sc.nextInt()];
		for (int i = 0; i < ret.length; i++) {
			ret[i] = sc.nextInt();
		}
		return ret;
	}

	private void outputResult(boolean canDevideInHalf) {
		if (canDevideInHalf) {
			System.out.println("possible");
		} else {
			System.out.println("impossible");
		}
	}

}
0