結果

問題 No.4 おもりと天秤
ユーザー shinwisteriashinwisteria
提出日時 2017-03-28 10:47:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 148 ms / 5,000 ms
コード長 872 bytes
コンパイル時間 4,556 ms
コンパイル使用メモリ 74,736 KB
実行使用メモリ 56,820 KB
最終ジャッジ日時 2023-09-08 17:23:32
合計ジャッジ時間 8,293 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,988 KB
testcase_01 AC 122 ms
55,936 KB
testcase_02 AC 123 ms
56,436 KB
testcase_03 AC 122 ms
56,080 KB
testcase_04 AC 123 ms
56,136 KB
testcase_05 AC 140 ms
56,008 KB
testcase_06 AC 119 ms
55,992 KB
testcase_07 AC 140 ms
56,636 KB
testcase_08 AC 131 ms
55,996 KB
testcase_09 AC 139 ms
56,528 KB
testcase_10 AC 141 ms
56,376 KB
testcase_11 AC 124 ms
55,988 KB
testcase_12 AC 123 ms
55,856 KB
testcase_13 AC 123 ms
56,028 KB
testcase_14 AC 121 ms
56,128 KB
testcase_15 AC 120 ms
56,388 KB
testcase_16 AC 124 ms
56,220 KB
testcase_17 AC 123 ms
56,100 KB
testcase_18 AC 139 ms
55,816 KB
testcase_19 AC 148 ms
56,144 KB
testcase_20 AC 142 ms
56,016 KB
testcase_21 AC 138 ms
55,976 KB
testcase_22 AC 138 ms
56,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class OmoritoTenbin {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int N = s.nextInt(),total = 0;
		int[] W = new int[N+1];
		W[0] = 0;
		for(int i = 1;i <= N ;i++){
			W[i] = s.nextInt();
			total += W[i];
		}
		s.close();
		Arrays.sort(W);
		if(total % 2 == 1){
			System.out.println("impossible");
		}else{
			total /=2;
			int[][] way = new int[total+1][2];
			way[0][0] = 1;
			for(int i = 0;i <= total;i++){
				for(int j = 1;j <= N;j++){
					if(i-W[j] >= 0 &&way[i-W[j]][0] == 1 && way[i-W[j]][1] < j){
						way[i][0] = 1;
						way[i][1] = j;
						break;
					}
				}
				//System.out.println(i + "  " + way[i][0] + "  " + way[i][1]);
			}
			if(way[total][0] == 1){
				System.out.println("possible");
			}else{
				System.out.println("impossible");
			}
		}
	}
}
0