結果

問題 No.497 入れ子の箱
ユーザー Kilisame
提出日時 2017-05-01 09:26:28
言語 Java
(openjdk 23)
結果
AC  
実行時間 262 ms / 5,000 ms
コード長 2,260 bytes
コンパイル時間 4,064 ms
コンパイル使用メモリ 78,984 KB
実行使用メモリ 56,084 KB
最終ジャッジ日時 2024-09-14 01:47:12
合計ジャッジ時間 12,137 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class NestedBox {

    private static int n;

    private static long[][] boxes;

    private static int[] checked;

    private static int max;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = Integer.parseInt(sc.next());
        boxes = new long[n][3];
        checked = new int[n];
        for (int i = 0; i < n; i++) {
            boxes[i][0] = Long.parseLong(sc.next());
            boxes[i][1] = Long.parseLong(sc.next());
            boxes[i][2] = Long.parseLong(sc.next());
            Arrays.sort(boxes[i]);
        }
        sc.close();
        Arrays.sort(boxes, new Comparator<long[]>() {
            @Override
            public int compare(long[] o1, long[] o2) {
                if (o1[0] < o2[0]) {
                    return -1;
                } else if (o1[0] > o2[0]) {
                    return 1;
                }
                if (o1[1] < o2[1]) {
                    return -1;
                } else if (o1[1] > o2[1]) {
                    return 1;
                }
                if (o1[2] < o2[2]) {
                    return -1;
                } else if (o1[2] > o2[2]) {
                    return 1;
                }
                return 0;
            }
        });
        max = 1;
        for (int i = 0; i < n; i++) {
            nest(i);
        }
        System.out.println(max);
    }

    private static int nest(int index) {
        if (checked[index] > 0) {
            return checked[index];
        }
        int maxNested = 1;
        for (int i = index + 1; i < n; i++) {
            int nested = 1;
            if (isIn(index, i)) {
                nested += nest(i);
            }
            if (nested > maxNested) {
                maxNested = nested;
            }
        }
        if (maxNested > max) {
            max = maxNested;
        }
        checked[index] = maxNested;
        return maxNested;
    }

    private static boolean isIn(int innerIndex, int outerIndex) {
        long[] inner = boxes[innerIndex];
        long[] outer = boxes[outerIndex];
        return inner[0] < outer[0] && inner[1] < outer[1] && inner[2] < outer[2];
    }

}
0