結果

問題 No.1583 Building Blocks
ユーザー tentententen
提出日時 2021-07-05 18:15:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 2,119 bytes
コンパイル時間 2,307 ms
コンパイル使用メモリ 78,012 KB
実行使用メモリ 61,080 KB
最終ジャッジ日時 2023-10-12 03:05:58
合計ジャッジ時間 7,464 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,672 KB
testcase_01 AC 46 ms
49,700 KB
testcase_02 AC 46 ms
49,744 KB
testcase_03 AC 90 ms
54,068 KB
testcase_04 AC 51 ms
49,748 KB
testcase_05 AC 48 ms
49,736 KB
testcase_06 AC 91 ms
54,344 KB
testcase_07 AC 93 ms
54,036 KB
testcase_08 AC 51 ms
49,676 KB
testcase_09 AC 107 ms
56,964 KB
testcase_10 AC 47 ms
49,780 KB
testcase_11 AC 46 ms
47,880 KB
testcase_12 AC 95 ms
54,224 KB
testcase_13 AC 99 ms
56,360 KB
testcase_14 AC 83 ms
52,272 KB
testcase_15 AC 95 ms
53,924 KB
testcase_16 AC 101 ms
56,396 KB
testcase_17 AC 102 ms
59,000 KB
testcase_18 AC 56 ms
49,860 KB
testcase_19 AC 110 ms
61,080 KB
testcase_20 AC 83 ms
51,848 KB
testcase_21 AC 77 ms
51,556 KB
testcase_22 AC 79 ms
50,684 KB
testcase_23 AC 51 ms
49,796 KB
testcase_24 AC 101 ms
56,384 KB
testcase_25 AC 43 ms
49,636 KB
testcase_26 AC 101 ms
56,744 KB
testcase_27 AC 75 ms
52,140 KB
testcase_28 AC 102 ms
56,424 KB
testcase_29 AC 56 ms
49,952 KB
testcase_30 AC 49 ms
49,716 KB
testcase_31 AC 99 ms
56,952 KB
testcase_32 AC 100 ms
54,028 KB
testcase_33 AC 112 ms
60,988 KB
testcase_34 AC 46 ms
49,748 KB
testcase_35 AC 95 ms
54,496 KB
testcase_36 AC 105 ms
56,656 KB
testcase_37 AC 55 ms
50,400 KB
testcase_38 AC 44 ms
49,808 KB
testcase_39 AC 45 ms
49,648 KB
testcase_40 AC 50 ms
49,756 KB
testcase_41 AC 44 ms
49,744 KB
testcase_42 AC 44 ms
50,320 KB
testcase_43 AC 41 ms
49,636 KB
testcase_44 AC 42 ms
49,996 KB
testcase_45 AC 44 ms
49,668 KB
権限があれば一括ダウンロードができます

ソースコード

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