結果

問題 No.2167 Fibonacci Knapsack
ユーザー tentententen
提出日時 2022-12-26 15:21:12
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,450 bytes
コンパイル時間 2,728 ms
コンパイル使用メモリ 83,892 KB
実行使用メモリ 54,136 KB
最終ジャッジ日時 2024-05-01 01:07:56
合計ジャッジ時間 7,859 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,472 KB
testcase_01 AC 53 ms
50,348 KB
testcase_02 AC 98 ms
54,136 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static long[] weights;
    static long[] values;
    static ArrayList<HashMap<Long, Long>> dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int t = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (t-- > 0) {
            int n = sc.nextInt();
            long w = sc.nextLong();
            weights = new long[n];
            values = new long[n];
            dp = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                weights[i] = sc.nextLong();
                dp.add(new HashMap<>());
            }
            values[0] = 1;
            if (n > 1) {
                values[1] = 2;
            }
            for (int i = 2; i < n; i++) {
                values[i] = values[i - 1] + values[i - 2];
            }
            sb.append(dfw(n - 1, w)).append("\n");
        }
        System.out.print(sb);
    }
    
    static long dfw(int idx, long w) {
        if (w < 0) {
            return Long.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (!dp.get(idx).containsKey(w)) {
            dp.get(idx).put(w, Math.max(dfw(idx - 1, w), dfw(idx - 1, w - weights[idx]) + values[idx]));
        }
        return dp.get(idx).get(w);
    }
    
}
class Utilities {
    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