結果

問題 No.777 再帰的ケーキ
ユーザー tentententen
提出日時 2020-12-16 18:50:23
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,137 bytes
コンパイル時間 2,651 ms
コンパイル使用メモリ 79,424 KB
実行使用メモリ 105,872 KB
最終ジャッジ日時 2023-10-20 09:46:00
合計ジャッジ時間 12,271 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
56,340 KB
testcase_01 AC 127 ms
57,220 KB
testcase_02 AC 127 ms
57,124 KB
testcase_03 AC 129 ms
57,664 KB
testcase_04 AC 135 ms
57,464 KB
testcase_05 AC 134 ms
57,596 KB
testcase_06 AC 123 ms
56,308 KB
testcase_07 AC 140 ms
57,628 KB
testcase_08 AC 135 ms
57,672 KB
testcase_09 AC 129 ms
57,528 KB
testcase_10 AC 128 ms
57,416 KB
testcase_11 AC 115 ms
56,344 KB
testcase_12 AC 134 ms
57,564 KB
testcase_13 AC 129 ms
57,672 KB
testcase_14 AC 128 ms
57,608 KB
testcase_15 AC 129 ms
57,600 KB
testcase_16 AC 128 ms
57,684 KB
testcase_17 AC 134 ms
57,600 KB
testcase_18 AC 135 ms
57,536 KB
testcase_19 AC 144 ms
57,588 KB
testcase_20 AC 124 ms
56,348 KB
testcase_21 AC 260 ms
61,404 KB
testcase_22 AC 256 ms
61,148 KB
testcase_23 AC 220 ms
60,580 KB
testcase_24 AC 213 ms
60,276 KB
testcase_25 AC 271 ms
61,396 KB
testcase_26 AC 262 ms
60,968 KB
testcase_27 AC 224 ms
60,656 KB
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Cake[] cakes = new Cake[n];
		for (int i = 0; i < n; i++) {
		    cakes[i] = new Cake(sc.nextInt(), sc.nextInt(), sc.nextInt());
		}
		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 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