結果

問題 No.1594 Three Classes
ユーザー tentententen
提出日時 2024-02-09 13:27:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 2,042 bytes
コンパイル時間 6,418 ms
コンパイル使用メモリ 90,924 KB
実行使用メモリ 56,188 KB
最終ジャッジ日時 2024-02-09 13:27:33
合計ジャッジ時間 9,417 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
55,776 KB
testcase_01 AC 91 ms
55,280 KB
testcase_02 AC 96 ms
55,388 KB
testcase_03 AC 104 ms
56,188 KB
testcase_04 AC 61 ms
54,764 KB
testcase_05 AC 105 ms
55,912 KB
testcase_06 AC 114 ms
56,052 KB
testcase_07 AC 107 ms
56,056 KB
testcase_08 AC 108 ms
56,160 KB
testcase_09 AC 103 ms
55,908 KB
testcase_10 AC 107 ms
55,912 KB
testcase_11 AC 105 ms
55,884 KB
testcase_12 AC 103 ms
55,904 KB
testcase_13 AC 86 ms
55,632 KB
testcase_14 AC 112 ms
56,028 KB
testcase_15 AC 112 ms
56,044 KB
testcase_16 AC 104 ms
55,648 KB
testcase_17 AC 123 ms
56,156 KB
testcase_18 AC 88 ms
55,644 KB
testcase_19 AC 67 ms
54,876 KB
testcase_20 AC 82 ms
53,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        List<Integer> list = Stream.generate(sc::nextInt).limit(n).toList();
        int sum = list.stream().mapToInt(Integer::intValue).sum();
        if (sum % 3 > 0) {
            System.out.println("No");
            return;
        }
        final int sumValue = sum / 3;
        int[] dp = new int[1 << n];
        List<Integer> total = IntStream.range(1, 1 << n).mapToObj(i -> {
            int idx = IntStream.range(0, n).filter(j -> (i & (1 << j)) > 0).findFirst().orElse(0);
            dp[i] = dp[i ^ (1 << idx)] + list.get(idx);
            return (dp[i] == sumValue) ?  i : 0;
        }).filter(i -> i > 0).toList();
        boolean isPresent = total.stream().anyMatch(a -> {
            return total.stream().filter(b -> a < b && (a & b) == 0).anyMatch(b -> {
                return total.stream().anyMatch(c -> b < c && ((a | b) & c) == 0);
            });
        });
        System.out.println(isPresent ? "Yes" : "No");
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0