結果

問題 No.497 入れ子の箱
ユーザー tentententen
提出日時 2021-11-12 08:47:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 160 ms / 5,000 ms
コード長 2,861 bytes
コンパイル時間 2,356 ms
コンパイル使用メモリ 78,572 KB
実行使用メモリ 47,396 KB
最終ジャッジ日時 2024-05-03 14:10:42
合計ジャッジ時間 7,616 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
37,116 KB
testcase_01 AC 52 ms
37,156 KB
testcase_02 AC 53 ms
37,244 KB
testcase_03 AC 125 ms
47,184 KB
testcase_04 AC 146 ms
47,256 KB
testcase_05 AC 157 ms
47,108 KB
testcase_06 AC 146 ms
47,260 KB
testcase_07 AC 148 ms
47,264 KB
testcase_08 AC 147 ms
47,108 KB
testcase_09 AC 144 ms
47,396 KB
testcase_10 AC 141 ms
47,120 KB
testcase_11 AC 145 ms
47,076 KB
testcase_12 AC 151 ms
47,108 KB
testcase_13 AC 147 ms
47,284 KB
testcase_14 AC 146 ms
47,100 KB
testcase_15 AC 152 ms
47,180 KB
testcase_16 AC 150 ms
47,268 KB
testcase_17 AC 160 ms
47,300 KB
testcase_18 AC 144 ms
47,064 KB
testcase_19 AC 135 ms
46,968 KB
testcase_20 AC 149 ms
47,116 KB
testcase_21 AC 146 ms
47,196 KB
testcase_22 AC 151 ms
47,056 KB
testcase_23 AC 111 ms
46,528 KB
testcase_24 AC 129 ms
46,556 KB
testcase_25 AC 52 ms
37,084 KB
testcase_26 AC 51 ms
37,084 KB
testcase_27 AC 144 ms
47,204 KB
testcase_28 AC 139 ms
47,228 KB
testcase_29 AC 143 ms
47,088 KB
testcase_30 AC 126 ms
46,412 KB
testcase_31 AC 131 ms
46,476 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