結果

問題 No.4 おもりと天秤
ユーザー maruyuki95maruyuki95
提出日時 2020-07-05 21:24:58
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,619 bytes
コンパイル時間 2,242 ms
コンパイル使用メモリ 79,068 KB
実行使用メモリ 67,172 KB
最終ジャッジ日時 2023-10-24 05:05:56
合計ジャッジ時間 11,778 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
57,260 KB
testcase_01 AC 125 ms
57,492 KB
testcase_02 AC 149 ms
59,664 KB
testcase_03 AC 129 ms
57,212 KB
testcase_04 AC 132 ms
57,612 KB
testcase_05 AC 141 ms
57,680 KB
testcase_06 AC 131 ms
57,544 KB
testcase_07 AC 142 ms
57,716 KB
testcase_08 AC 136 ms
57,592 KB
testcase_09 AC 138 ms
55,640 KB
testcase_10 AC 140 ms
57,588 KB
testcase_11 AC 132 ms
57,532 KB
testcase_12 AC 131 ms
57,356 KB
testcase_13 AC 127 ms
57,232 KB
testcase_14 AC 130 ms
57,660 KB
testcase_15 AC 129 ms
57,660 KB
testcase_16 AC 128 ms
55,092 KB
testcase_17 AC 131 ms
57,728 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

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 halfTotalWeight = totalWeight /2 ;
		return canMakeWeight(convertToList(weights), halfTotalWeight);
	}

	private boolean canMakeWeight(List<Integer> weights, int goalWeight) {
		List<Integer> copyList = new ArrayList<>(weights);
		
		for (Integer weight : weights) {
			if (weight == goalWeight) {
				return true;
			} 
			
			
			copyList.remove(weight);
			if (weight > goalWeight) {
				continue;
			}
			
			if (canMakeWeight(copyList, goalWeight - weight)) {
				return true;
			}
		}
		return false;
	}

	private List<Integer> convertToList(int[] array) {
		List<Integer> ret = new ArrayList<>();
		for (int i : array) {
			ret.add(i);
		}
		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