結果

問題 No.412 花火大会
ユーザー htensaihtensai
提出日時 2020-01-09 10:16:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 4,270 bytes
コンパイル時間 2,236 ms
コンパイル使用メモリ 76,688 KB
実行使用メモリ 51,644 KB
最終ジャッジ日時 2023-08-15 01:09:51
合計ジャッジ時間 4,416 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,444 KB
testcase_01 AC 43 ms
49,280 KB
testcase_02 AC 44 ms
49,464 KB
testcase_03 AC 43 ms
49,452 KB
testcase_04 AC 42 ms
49,484 KB
testcase_05 AC 43 ms
51,644 KB
testcase_06 AC 44 ms
49,388 KB
testcase_07 AC 44 ms
49,656 KB
testcase_08 AC 43 ms
49,784 KB
testcase_09 AC 42 ms
49,760 KB
testcase_10 AC 42 ms
49,476 KB
testcase_11 AC 43 ms
49,280 KB
testcase_12 AC 42 ms
49,920 KB
testcase_13 AC 43 ms
49,468 KB
testcase_14 AC 44 ms
49,428 KB
testcase_15 AC 43 ms
49,420 KB
testcase_16 AC 43 ms
49,368 KB
testcase_17 AC 42 ms
49,420 KB
testcase_18 AC 43 ms
49,452 KB
testcase_19 AC 43 ms
49,536 KB
testcase_20 AC 43 ms
49,388 KB
testcase_21 AC 43 ms
49,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[] families;
    static int[] sheets;
    static int[][] dp; //idx, 1, 2, 3
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 3);
        families = new int[3];
        for (int i = 0; i < 3; i++) {
            families[i] = Integer.parseInt(first[i]);
        }
        Arrays.sort(families);
        int n = Integer.parseInt(br.readLine());
        sheets = new int[n + 1];
        String[] line = br.readLine().split(" ", n);
        for (int i = 0; i < n; i++) {
            sheets[i + 1] = Integer.parseInt(line[i]);
        }
        Arrays.sort(sheets);
        dp = new int[n + 1][3 * 16 + 3 * 4 + 3 + 1];
        for (int i = 0; i <= n; i++) {
            Arrays.fill(dp[i], -1);
        }
        System.out.print(dfw(n, 0));
    }
    
    static int dfw(int idx, int value) {
        if (idx <= 0) {
            return 0;
        }
        if (dp[idx][value] != -1) {
            return dp[idx][value];
        }
        int s = value / 16;
        int m = (value % 16) / 4;
        int l = value % 4;
        dp[idx][value] = dfw(idx - 1, value);
        if (sheets[idx] >= families[0]) {
            s = Math.min(s + 1, 3);
        }
        if (sheets[idx] >= families[1]) {
            m = Math.min(m + 1, 3);
        }
        if (sheets[idx] >= families[2]) {
            l = Math.min(l + 1, 3);
        }
        int next = s * 16 + m * 4 + l;
        if (s >= 3 && m >= 2 && l >= 1) {
            dp[idx][value] += (int)(Math.pow(2, idx - 1));
        } else {
            dp[idx][value] += dfw(idx - 1, next);
        }
        return dp[idx][value];
        
    }
    static class Bridge {
        int left;
        int right;
        
        public Bridge (String arg) {
            String[] arr = arg.split(" ", 2);
            left = Integer.parseInt(arr[0]) - 1;
            right = Integer.parseInt(arr[1]) - 1;
        }
        
        public int hashCode() {
            return left + right;
        }
        
        public boolean equals(Object o) {
            Bridge b = (Bridge)o;
            return b.left == left && b.right == right;
        }
    }
    
    static class UnionFindTree {
        int[] parents;
        HashMap<Integer, HashSet<Integer>> map = new HashMap<>();
        HashSet<Integer> nothing = new HashSet<>();
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int x) {
            if (parents[x] == x) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public void unite(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx == yy) {
                return;
            }
            int small = Math.min(xx, yy);
            int large = Math.max(xx, yy);
            parents[large] = small;
        }
        
        public void unite(Bridge b) {
            unite(b.left, b.right);
        }
        
        public HashSet<Integer> uniteNext(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx == yy) {
                return nothing;
            }
            int small = Math.min(xx, yy);
            int large = Math.max(xx, yy);
            parents[large] = small;
            if (small == 0) {
                return map.get(large);
            } else {
                map.get(small).addAll(map.get(large));
                return nothing;
            }
        }
        
        public HashSet<Integer> uniteNext(Bridge b) {
            return uniteNext(b.left, b.right);
        }
        
        public HashSet<Integer> prepare() {
            for (int i = 0; i < parents.length; i++) {
                int x = find(i);
                if (!map.containsKey(x)) {
                    map.put(x, new HashSet<Integer>());
                }
                map.get(x).add(i);
            }
            return map.get(0);
        }
    }
    
}
0