結果

問題 No.247 線形計画問題もどき
ユーザー tentententen
提出日時 2023-06-13 14:29:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 2,286 bytes
コンパイル時間 4,829 ms
コンパイル使用メモリ 80,620 KB
実行使用メモリ 102,676 KB
最終ジャッジ日時 2023-09-03 23:00:48
合計ジャッジ時間 6,410 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
64,080 KB
testcase_01 AC 41 ms
49,240 KB
testcase_02 AC 220 ms
102,676 KB
testcase_03 AC 42 ms
49,352 KB
testcase_04 AC 41 ms
47,472 KB
testcase_05 AC 42 ms
49,280 KB
testcase_06 AC 41 ms
49,252 KB
testcase_07 AC 49 ms
49,404 KB
testcase_08 AC 63 ms
63,356 KB
testcase_09 AC 44 ms
49,288 KB
testcase_10 AC 43 ms
49,392 KB
testcase_11 AC 41 ms
49,368 KB
testcase_12 AC 40 ms
49,292 KB
testcase_13 AC 40 ms
49,212 KB
testcase_14 AC 40 ms
47,524 KB
testcase_15 AC 39 ms
49,280 KB
testcase_16 AC 40 ms
49,392 KB
testcase_17 AC 87 ms
60,544 KB
testcase_18 AC 66 ms
52,892 KB
testcase_19 AC 120 ms
83,352 KB
testcase_20 AC 184 ms
82,616 KB
testcase_21 AC 56 ms
53,376 KB
testcase_22 AC 76 ms
59,696 KB
testcase_23 AC 61 ms
55,244 KB
testcase_24 AC 185 ms
82,520 KB
testcase_25 AC 143 ms
76,076 KB
testcase_26 AC 61 ms
55,284 KB
testcase_27 AC 74 ms
59,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[] values;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int c = sc.nextInt();
        int n = sc.nextInt();
        values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        dp = new int[n][c + 1];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        int ans = dfw(n - 1, c);
        if (ans >= Integer.MAX_VALUE / 2) {
            ans = -1;
        }
        System.out.println(ans);
    }
    
    static int dfw(int idx, int v) {
        if (v < 0) {
            return Integer.MAX_VALUE / 2;
        }
        if (v == 0) {
            return 0;
        }
        if (idx < 0) {
            return Integer.MAX_VALUE / 2;
        }
        if (dp[idx][v] < 0) {
            dp[idx][v] = Math.min(dfw(idx - 1, v), dfw(idx, v - values[idx]) + 1);
        }
        return dp[idx][v];
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0