結果

問題 No.4 おもりと天秤
ユーザー uafr_csuafr_cs
提出日時 2015-06-09 02:56:23
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 980 bytes
コンパイル時間 3,686 ms
コンパイル使用メモリ 75,432 KB
実行使用メモリ 56,420 KB
最終ジャッジ日時 2023-09-20 20:06:48
合計ジャッジ時間 6,858 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
56,148 KB
testcase_01 AC 122 ms
55,816 KB
testcase_02 AC 121 ms
55,872 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 123 ms
55,748 KB
testcase_07 WA -
testcase_08 AC 131 ms
55,500 KB
testcase_09 AC 146 ms
56,016 KB
testcase_10 WA -
testcase_11 AC 123 ms
56,180 KB
testcase_12 AC 125 ms
55,748 KB
testcase_13 AC 125 ms
55,988 KB
testcase_14 AC 124 ms
56,304 KB
testcase_15 AC 125 ms
55,820 KB
testcase_16 WA -
testcase_17 AC 122 ms
55,756 KB
testcase_18 AC 141 ms
56,000 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	
	// どんな個数でも錘の合計の半分の重さを達成できれば, 残りの錘も半分の重さである.
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		
		int[] arr = new int[N];
		for(int i = 0; i < N; i++){
			arr[i] = sc.nextInt();
		}
		final int sum = Arrays.stream(arr).sum();
		
		if(sum % 2 != 0){
			System.out.println("impossible");
		}else{
			boolean[][] DP = new boolean[N + 1][sum + 1];
			
			DP[0][0] = true;
			for(int i = 0; i < N; i++){
				
				for(int j = sum - arr[i]; j >= 0; j--){
					if(DP[i][j]){
						DP[i + 1][j + arr[i]] = true;
					}
				}
			}
			
			boolean ok = false;
			for(int i = 0; i < N; i++){
				if(DP[i][sum / 2]){
					ok = true;
					break;
				}
			}
			
			System.out.println(ok ? "possible" : "impossible");
		}
		
	}
	
}
0