結果

問題 No.4 おもりと天秤
ユーザー tentententen
提出日時 2023-11-15 15:55:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 87 ms / 5,000 ms
コード長 2,199 bytes
コンパイル時間 2,824 ms
コンパイル使用メモリ 90,716 KB
実行使用メモリ 56,176 KB
最終ジャッジ日時 2023-11-15 15:55:57
合計ジャッジ時間 5,845 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
55,276 KB
testcase_01 AC 56 ms
53,980 KB
testcase_02 AC 55 ms
53,984 KB
testcase_03 AC 73 ms
55,412 KB
testcase_04 AC 74 ms
55,284 KB
testcase_05 AC 87 ms
56,172 KB
testcase_06 AC 54 ms
53,996 KB
testcase_07 AC 86 ms
56,176 KB
testcase_08 AC 55 ms
53,996 KB
testcase_09 AC 75 ms
55,280 KB
testcase_10 AC 87 ms
56,164 KB
testcase_11 AC 55 ms
53,972 KB
testcase_12 AC 55 ms
53,980 KB
testcase_13 AC 55 ms
53,980 KB
testcase_14 AC 55 ms
54,008 KB
testcase_15 AC 55 ms
53,996 KB
testcase_16 AC 73 ms
55,276 KB
testcase_17 AC 74 ms
55,288 KB
testcase_18 AC 71 ms
54,004 KB
testcase_19 AC 86 ms
56,168 KB
testcase_20 AC 86 ms
56,160 KB
testcase_21 AC 87 ms
56,156 KB
testcase_22 AC 83 ms
55,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[] weights;
    static boolean[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        weights = new int[n];
        int total = 0;
        for (int i = 0; i < n; i++) {
            weights[i] = sc.nextInt();
            total += weights[i];
        }
        if (total % 2 == 0) {
            dp = new boolean[n][total / 2 + 1];
            dfw(n - 1, total / 2);
        }
        System.out.println("impossible");
    }
    
    static boolean dfw(int idx, int v) {
        if (v == 0) {
            System.out.println("possible");
            System.exit(0);
        }
        if (v < 0 || idx < 0) {
            return true;
        }
        if (!dp[idx][v]) {
            dp[idx][v] = (dfw(idx - 1,  v) & dfw(idx - 1, v - weights[idx]));
        }
        return true;
    }
    
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0