結果

問題 No.1826 Fruits Collecting
ユーザー tenten
提出日時 2022-02-15 11:14:52
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,420 ms / 2,000 ms
コード長 2,582 bytes
コンパイル時間 3,080 ms
コンパイル使用メモリ 83,044 KB
実行使用メモリ 69,612 KB
最終ジャッジ日時 2024-06-29 07:05:46
合計ジャッジ時間 38,427 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        ArrayList<Fruit> fruits = new ArrayList<>();
        TreeMap<Integer, Integer> compress = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            Fruit f = new Fruit(sc.nextInt(), sc.nextInt(), sc.nextInt());
            if (f.enable()) {
                fruits.add(f);
                compress.put(f.right, null);
            }
        }
        Collections.sort(fruits);
        int idx = 1;
        for (int x : compress.keySet()) {
            compress.put(x, idx++);
        }
        long ans = 0;
        TreeMap<Integer, Long> maxes = new TreeMap<>();
        maxes.put(0, 0L);
        for (Fruit f : fruits) {
            Integer position = compress.get(f.right);
            long next = maxes.floorEntry(position).getValue() + f.value;
            ans = Math.max(ans, next);
            maxes.put(position, next);
            while ((position = maxes.higherKey(position)) != null) {
                if (maxes.get(position) > next) {
                    break;
                }
                maxes.remove(position);
            }
        }
        System.out.println(ans);
    }
    static class Fruit implements Comparable<Fruit> {
        int left;
        int right;
        int value;
        
        public Fruit(int t, int x, int value) {
            left = t + x;
            right = t - x;
            this.value = value;
        }
        
        public int compareTo(Fruit another) {
            if (left == another.left) {
                return right - another.right;
            } else {
                return left - another.left;
            }
        }
        
        public boolean enable() {
            return left >= 0 && right >= 0;
        }
        
        public String toString() {
            return left + ":" + right + ":" + value;
        }
    }
}
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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0