結果

問題 No.497 入れ子の箱
ユーザー tentententen
提出日時 2021-11-12 08:47:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 159 ms / 5,000 ms
コード長 2,861 bytes
コンパイル時間 3,716 ms
コンパイル使用メモリ 74,220 KB
実行使用メモリ 60,168 KB
最終ジャッジ日時 2023-08-16 04:54:09
合計ジャッジ時間 7,548 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,716 KB
testcase_01 AC 40 ms
49,724 KB
testcase_02 AC 44 ms
49,680 KB
testcase_03 AC 101 ms
57,888 KB
testcase_04 AC 123 ms
58,184 KB
testcase_05 AC 130 ms
58,280 KB
testcase_06 AC 128 ms
58,424 KB
testcase_07 AC 127 ms
58,332 KB
testcase_08 AC 128 ms
57,696 KB
testcase_09 AC 132 ms
58,252 KB
testcase_10 AC 126 ms
58,376 KB
testcase_11 AC 125 ms
60,168 KB
testcase_12 AC 141 ms
58,400 KB
testcase_13 AC 140 ms
57,816 KB
testcase_14 AC 121 ms
57,688 KB
testcase_15 AC 145 ms
58,552 KB
testcase_16 AC 138 ms
57,888 KB
testcase_17 AC 140 ms
58,576 KB
testcase_18 AC 130 ms
58,300 KB
testcase_19 AC 136 ms
58,304 KB
testcase_20 AC 159 ms
58,536 KB
testcase_21 AC 149 ms
58,492 KB
testcase_22 AC 159 ms
58,044 KB
testcase_23 AC 94 ms
57,764 KB
testcase_24 AC 107 ms
57,556 KB
testcase_25 AC 42 ms
49,616 KB
testcase_26 AC 41 ms
50,000 KB
testcase_27 AC 134 ms
58,348 KB
testcase_28 AC 129 ms
58,908 KB
testcase_29 AC 144 ms
58,116 KB
testcase_30 AC 123 ms
57,620 KB
testcase_31 AC 112 ms
57,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static Box[] boxes;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        boxes = new Box[n + 1];
        for (int i = 0; i < n; i++) {
            boxes[i] = new Box(sc.nextInt(), sc.nextInt(), sc.nextInt());
        }
        boxes[n] = new Box(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
        Arrays.sort(boxes);
        dp = new int[n + 1][n + 1];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(n - 1, n));
    }
    
    static int dfw(int idx, int prev) {
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][prev] < 0) {
            dp[idx][prev] = dfw(idx - 1, prev);
            if (boxes[idx].x < boxes[prev].x && boxes[idx].y < boxes[prev].y && boxes[idx].z < boxes[prev].z) {
                dp[idx][prev] = Math.max(dp[idx][prev], dfw(idx - 1, idx) + 1);
            }
        }
        return dp[idx][prev];
    }
    
    static class Box implements Comparable<Box> {
        int x;
        int y;
        int z;
        
        public Box(int x, int y, int z) {
            this.x = x;
            this.y = y;
            this.z = z;
            sort();
        }
        
        private void sort() {
            if (y > z) {
                int tmp = z;
                z = y;
                y = tmp;
            }
            if (x > y) {
                int tmp = y;
                y = x;
                x = tmp;
            }
            if (y > z) {
                int tmp = z;
                z = y;
                y = tmp;
            }
        }
        
        public int compareTo(Box another) {
            if (x == another.x) {
                if (y == another.y) {
                    return z - another.z;
                } else {
                    return y - another.y;
                }
            } else {
                return x - another.x;
            }
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0