結果

問題 No.4 おもりと天秤
ユーザー htensaihtensai
提出日時 2019-12-26 18:26:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 171 ms / 5,000 ms
コード長 1,246 bytes
コンパイル時間 2,044 ms
コンパイル使用メモリ 77,680 KB
実行使用メモリ 54,548 KB
最終ジャッジ日時 2024-04-15 04:44:35
合計ジャッジ時間 6,245 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
54,148 KB
testcase_01 AC 130 ms
53,996 KB
testcase_02 AC 129 ms
53,996 KB
testcase_03 AC 130 ms
54,260 KB
testcase_04 AC 133 ms
54,168 KB
testcase_05 AC 161 ms
54,476 KB
testcase_06 AC 130 ms
54,044 KB
testcase_07 AC 162 ms
54,420 KB
testcase_08 AC 137 ms
54,064 KB
testcase_09 AC 149 ms
54,296 KB
testcase_10 AC 164 ms
54,488 KB
testcase_11 AC 135 ms
54,108 KB
testcase_12 AC 130 ms
54,032 KB
testcase_13 AC 131 ms
53,940 KB
testcase_14 AC 130 ms
54,204 KB
testcase_15 AC 132 ms
54,132 KB
testcase_16 AC 131 ms
54,024 KB
testcase_17 AC 130 ms
53,956 KB
testcase_18 AC 170 ms
54,232 KB
testcase_19 AC 171 ms
54,108 KB
testcase_20 AC 160 ms
54,260 KB
testcase_21 AC 171 ms
54,288 KB
testcase_22 AC 168 ms
54,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.math.*;

public class Main {
    static boolean[][] dp;
    static boolean[][] used;
    static int[] arr;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int total = 0;
        arr = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            arr[i] = sc.nextInt();
            total += arr[i];
        }
        if (total % 2 == 1) {
            System.out.println("impossible");
            return;
        }
        dp = new boolean[n + 1][total / 2 + 1];
        used = new boolean[n + 1][total / 2 + 1];
        if (dfw(n, total / 2)) {
            System.out.println("possible");
        } else {
            System.out.println("impossible");
        }
    }
    
    static boolean dfw(int idx, int value) {
        if (value < 0) {
            return false;
        }
        if (value == 0) {
            return true;
        }
        if (idx == 0) {
            return false;
        }
        if (used[idx][value]) {
            return dp[idx][value];
        }
        used[idx][value] = true;
        dp[idx][value] = (dfw(idx - 1, value) | dfw(idx - 1, value - arr[idx]));
        return dp[idx][value];
    }
}
0