結果

問題 No.1583 Building Blocks
ユーザー tenten
提出日時 2021-07-05 18:15:49
言語 Java
(openjdk 23)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 2,119 bytes
コンパイル時間 4,137 ms
コンパイル使用メモリ 83,808 KB
実行使用メモリ 60,960 KB
最終ジャッジ日時 2024-09-14 02:52:49
合計ジャッジ時間 8,472 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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();
        Block[] blocks = new Block[n];
        for (int i = 0; i < n; i++) {
            blocks[i] = new Block(sc.nextInt(), sc.nextInt());
        }
        Arrays.sort(blocks);
        long[][] dp = new long[n + 1][n + 1];
        for (long[] arr : dp) {
            Arrays.fill(arr, Long.MAX_VALUE / 2);
        }
        dp[0][0] = 0;
        for (int i = 1; i <= n; i++) {
            dp[i][0] = 0;
            for (int j = 1; j <= i; j++) {
                dp[i][j] = Math.min(dp[i][j], dp[i - 1][j]);
                if (dp[i - 1][j - 1] <= blocks[i - 1].limit) {
                    dp[i][j] = Math.min(dp[i][j], dp[i - 1][j - 1] + blocks[i - 1].weight);
                }
            }
        }
        for (int i = n; i >= 0; i--) {
            if (dp[n][i] < Long.MAX_VALUE / 2) {
                System.out.println(i);
                return;
            }
        }
    }

    static class Block implements Comparable<Block> {
        int weight;
        int limit;
        
        public Block(int weight, int limit) {
            this.weight = weight;
            this.limit = limit;
        }
        
        public int compareTo(Block another) {
            return - another.weight - another.limit + weight + limit;
        }
        
        public String toString() {
            return weight + ":" + limit;
        }
    }
}


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