結果

問題 No.4 おもりと天秤
ユーザー mastersatoshimastersatoshi
提出日時 2015-08-01 11:33:12
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,337 bytes
コンパイル時間 1,980 ms
コンパイル使用メモリ 73,808 KB
実行使用メモリ 50,292 KB
最終ジャッジ日時 2023-09-25 00:04:59
合計ジャッジ時間 4,013 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
49,272 KB
testcase_01 AC 40 ms
49,312 KB
testcase_02 AC 41 ms
49,404 KB
testcase_03 AC 42 ms
49,544 KB
testcase_04 AC 40 ms
49,268 KB
testcase_05 AC 48 ms
49,996 KB
testcase_06 AC 43 ms
49,196 KB
testcase_07 AC 47 ms
50,040 KB
testcase_08 AC 40 ms
47,216 KB
testcase_09 AC 49 ms
50,292 KB
testcase_10 AC 48 ms
50,164 KB
testcase_11 WA -
testcase_12 AC 41 ms
49,420 KB
testcase_13 AC 41 ms
49,224 KB
testcase_14 AC 40 ms
49,508 KB
testcase_15 AC 40 ms
49,428 KB
testcase_16 AC 40 ms
49,368 KB
testcase_17 AC 40 ms
49,516 KB
testcase_18 AC 52 ms
49,856 KB
testcase_19 AC 46 ms
49,564 KB
testcase_20 AC 45 ms
49,396 KB
testcase_21 AC 46 ms
49,448 KB
testcase_22 AC 45 ms
49,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());

        String[] strW = br.readLine().split(" ");

        int[] W = new int[strW.length];
        int sum = 0;
        for (int i = 0; i < strW.length; i++) {
            W[i] = Integer.parseInt(strW[i]);
            sum += W[i];
        }

        if (sum % 2 != 0) {
            System.out.println("impossible");
            return;
        }

        int[] d = new int[sum / 2 + 1];

        Arrays.fill(d, Integer.MAX_VALUE);
        d[0] = -1;

        for (int i = 0; i < W.length; i++) {
            int tmpW = W[i];
            for (int j = 0; j < d.length - tmpW; j++) {
                if (d[j] != Integer.MAX_VALUE) {
                    if (sum / 2 == j + tmpW) {
                        System.out.println("possible");
                        return;
                    } else if (j + tmpW < d.length
                            && d[j] != i
                            && d[j + tmpW] == Integer.MAX_VALUE) {
                        d[j + tmpW] = i;
                    }
                }
            }
        }

        System.out.println("impossible");
    }
}
0