結果

問題 No.247 線形計画問題もどき
ユーザー tentententen
提出日時 2023-05-01 18:33:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 273 ms / 2,000 ms
コード長 2,321 bytes
コンパイル時間 2,624 ms
コンパイル使用メモリ 84,928 KB
実行使用メモリ 99,984 KB
最終ジャッジ日時 2024-04-30 18:19:22
合計ジャッジ時間 6,522 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
63,900 KB
testcase_01 AC 59 ms
50,480 KB
testcase_02 AC 254 ms
99,984 KB
testcase_03 AC 58 ms
50,156 KB
testcase_04 AC 59 ms
50,328 KB
testcase_05 AC 60 ms
50,564 KB
testcase_06 AC 58 ms
50,380 KB
testcase_07 AC 65 ms
50,472 KB
testcase_08 AC 78 ms
63,132 KB
testcase_09 AC 59 ms
50,356 KB
testcase_10 AC 59 ms
50,016 KB
testcase_11 AC 57 ms
50,364 KB
testcase_12 AC 59 ms
50,472 KB
testcase_13 AC 58 ms
50,536 KB
testcase_14 AC 59 ms
50,264 KB
testcase_15 AC 58 ms
50,376 KB
testcase_16 AC 58 ms
50,488 KB
testcase_17 AC 99 ms
61,216 KB
testcase_18 AC 68 ms
52,576 KB
testcase_19 AC 155 ms
83,524 KB
testcase_20 AC 237 ms
82,476 KB
testcase_21 AC 80 ms
53,072 KB
testcase_22 AC 99 ms
56,688 KB
testcase_23 AC 81 ms
55,044 KB
testcase_24 AC 273 ms
82,356 KB
testcase_25 AC 181 ms
75,892 KB
testcase_26 AC 85 ms
55,572 KB
testcase_27 AC 96 ms
60,452 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) {
            System.out.println(-1);
        } else {
            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