結果

問題 No.777 再帰的ケーキ
ユーザー tentententen
提出日時 2020-12-16 18:53:41
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,913 ms / 2,000 ms
コード長 2,436 bytes
コンパイル時間 4,539 ms
コンパイル使用メモリ 78,848 KB
実行使用メモリ 90,244 KB
最終ジャッジ日時 2023-10-20 09:46:27
合計ジャッジ時間 17,885 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
52,596 KB
testcase_01 AC 53 ms
53,620 KB
testcase_02 AC 56 ms
53,608 KB
testcase_03 AC 54 ms
53,616 KB
testcase_04 AC 54 ms
53,620 KB
testcase_05 AC 54 ms
53,620 KB
testcase_06 AC 54 ms
52,572 KB
testcase_07 AC 54 ms
53,628 KB
testcase_08 AC 57 ms
53,620 KB
testcase_09 AC 54 ms
53,624 KB
testcase_10 AC 53 ms
53,608 KB
testcase_11 AC 53 ms
53,632 KB
testcase_12 AC 55 ms
53,624 KB
testcase_13 AC 56 ms
53,620 KB
testcase_14 AC 54 ms
52,556 KB
testcase_15 AC 54 ms
53,616 KB
testcase_16 AC 55 ms
53,612 KB
testcase_17 AC 54 ms
53,624 KB
testcase_18 AC 54 ms
53,620 KB
testcase_19 AC 56 ms
53,648 KB
testcase_20 AC 54 ms
53,616 KB
testcase_21 AC 154 ms
56,600 KB
testcase_22 AC 158 ms
56,820 KB
testcase_23 AC 102 ms
54,764 KB
testcase_24 AC 95 ms
54,568 KB
testcase_25 AC 157 ms
56,956 KB
testcase_26 AC 158 ms
56,804 KB
testcase_27 AC 123 ms
56,520 KB
testcase_28 AC 1,838 ms
86,140 KB
testcase_29 AC 1,763 ms
86,084 KB
testcase_30 AC 1,913 ms
90,244 KB
testcase_31 AC 1,741 ms
84,152 KB
testcase_32 AC 862 ms
84,304 KB
testcase_33 AC 433 ms
62,896 KB
testcase_34 AC 516 ms
65,116 KB
testcase_35 AC 1,330 ms
78,840 KB
testcase_36 AC 868 ms
86,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		Cake[] cakes = new Cake[n];
		for (int i = 0; i < n; i++) {
		    cakes[i] = new Cake(br.readLine());
		}
		Arrays.sort(cakes);
		Cake.doCompress();
		int size = 1;
		long max = 0;
		BinaryIndexedTree bit = new BinaryIndexedTree(Cake.size);
		for (Cake c : cakes) {
		    long next = bit.getMax(c.getIdx() - 1) + c.cal;
		    max = Math.max(max, next);
		    bit.set(c.getIdx(), next);
		}
		System.out.println(max);
	}
	
	static class Cake implements Comparable<Cake> {
	    static int size = 1;
	    static TreeMap<Integer, Integer> compress = new TreeMap<>();
	    static void doCompress() {
    		for (int x : compress.keySet()) {
    		    compress.put(x, size);
    		    size++;
    		}
	    }
	    
	    int width;
	    int height;
	    int cal;
	    
	    public Cake(int width, int height, int cal) {
	        this.width = width;
	        this.height = height;
	        this.cal = cal;
	        compress.put(height, 0);
	    }
	    
	    public Cake(String line) {
	        this(line.split(" ", 3));
	    }
	    
	    public Cake(String[] element) {
	        this(Integer.parseInt(element[0]), Integer.parseInt(element[1]), Integer.parseInt(element[2]));
	    }
	    
	    public int getIdx() {
	        return compress.get(height);
	    }
	    
	    public int compareTo(Cake another) {
	        if (width == another.width) {
	            return another.height - height;
	        } else {
	            return width - another.width;
	        }
	    }
	}
}

class BinaryIndexedTree {
    int size;
    long[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new long[size];
    }
    
    public void set(int idx, long value) {
        int mask = 1;
        while (idx < size) {
            if ((idx & mask) != 0) {
                tree[idx] = Math.max(tree[idx], value);
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public long getMax(int x) {
        int mask = 1;
        long ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans = Math.max(ans, tree[x]);
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
}
0