結果

問題 No.247 線形計画問題もどき
ユーザー tentententen
提出日時 2023-01-27 12:50:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 2,244 bytes
コンパイル時間 4,731 ms
コンパイル使用メモリ 84,756 KB
実行使用メモリ 103,584 KB
最終ジャッジ日時 2023-09-10 08:32:45
合計ジャッジ時間 6,242 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
63,528 KB
testcase_01 AC 43 ms
49,212 KB
testcase_02 AC 198 ms
103,584 KB
testcase_03 AC 43 ms
49,340 KB
testcase_04 AC 42 ms
47,372 KB
testcase_05 AC 41 ms
49,252 KB
testcase_06 AC 42 ms
49,544 KB
testcase_07 AC 43 ms
49,208 KB
testcase_08 AC 59 ms
62,784 KB
testcase_09 AC 44 ms
49,388 KB
testcase_10 AC 43 ms
49,248 KB
testcase_11 AC 42 ms
49,188 KB
testcase_12 AC 42 ms
49,292 KB
testcase_13 AC 41 ms
47,840 KB
testcase_14 AC 42 ms
49,332 KB
testcase_15 AC 42 ms
49,432 KB
testcase_16 AC 42 ms
49,396 KB
testcase_17 AC 66 ms
60,192 KB
testcase_18 AC 66 ms
53,180 KB
testcase_19 AC 102 ms
82,488 KB
testcase_20 AC 179 ms
82,264 KB
testcase_21 AC 46 ms
51,280 KB
testcase_22 AC 72 ms
57,876 KB
testcase_23 AC 48 ms
53,704 KB
testcase_24 AC 184 ms
81,596 KB
testcase_25 AC 130 ms
75,620 KB
testcase_26 AC 47 ms
53,936 KB
testcase_27 AC 53 ms
58,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int[][] dp;
    static int[] values;
    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];
        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;
        }
        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