結果
問題 | No.4 おもりと天秤 |
ユーザー |
|
提出日時 | 2016-05-05 20:53:29 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 144 ms / 5,000 ms |
コード長 | 1,245 bytes |
コンパイル時間 | 2,206 ms |
コンパイル使用メモリ | 79,728 KB |
実行使用メモリ | 41,836 KB |
最終ジャッジ日時 | 2024-06-26 09:33:19 |
合計ジャッジ時間 | 6,062 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
package jp.fedom.challange.yuki.l2.q4; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<String> input = new ArrayList<>(); while (sc.hasNext()) { input.add(sc.nextLine()); } System.out.println(solve(input)); sc.close(); } static int[] Ws; static boolean[][] dp; public static String solve(List<String> in) { int N = Integer.valueOf(in.get(0)); String[] W = in.get(1).split(" "); boolean res = false; Ws = new int[N]; int total = 0; for (int i = 0; i < N; i++) { Ws[i] = Integer.valueOf(W[i]); total += Ws[i]; } Arrays.sort(Ws); if (total % 2 == 1) { res = false; } else { res = dfs(N, Ws, total / 2); } return res ? "possible" : "impossible"; } private static boolean dfs(int N, int[] a, int K) { dp = new boolean[N + 1][]; for (int i = 0; i < dp.length; i++) { dp[i] = new boolean[K + 1]; } dp[0][0] = true; for (int i = 0; i < N; i++) { for (int j = 0; j <= K; j++) { dp[i + 1][j] |= dp[i][j]; if (0 <= j - a[i]) { dp[i + 1][j] |= dp[i][j - a[i]]; } } } return dp[N][K]; } }