結果

問題 No.664 超能力者Aと株価予測
ユーザー htensaihtensai
提出日時 2020-02-06 17:19:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 56 ms / 4,000 ms
コード長 1,830 bytes
コンパイル時間 2,185 ms
コンパイル使用メモリ 77,784 KB
実行使用メモリ 53,236 KB
最終ジャッジ日時 2023-10-25 22:36:25
合計ジャッジ時間 3,664 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
53,220 KB
testcase_01 AC 53 ms
53,224 KB
testcase_02 AC 53 ms
53,236 KB
testcase_03 AC 51 ms
53,220 KB
testcase_04 AC 51 ms
53,224 KB
testcase_05 AC 52 ms
53,228 KB
testcase_06 AC 51 ms
53,228 KB
testcase_07 AC 51 ms
53,228 KB
testcase_08 AC 55 ms
53,232 KB
testcase_09 AC 52 ms
53,228 KB
testcase_10 AC 54 ms
53,236 KB
testcase_11 AC 55 ms
53,232 KB
testcase_12 AC 56 ms
53,232 KB
testcase_13 AC 54 ms
53,236 KB
testcase_14 AC 50 ms
52,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 3);
        int n = Integer.parseInt(first[0]);
        int m = Integer.parseInt(first[1]);
        int k = Integer.parseInt(first[2]);
        int[] prices = new int[n + 2];
        for (int i = 1; i <= n + 1; i++) {
            prices[i] = Integer.parseInt(br.readLine());
        }
        long[][] buyDp = new long[m + 1][n + 2];
        long[][] sellDp = new long[m + 1][n + 2];
        long[][] restDp = new long[m + 1][n + 2];
        Arrays.fill(sellDp[0], k);
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n + 1; j++) {
                long buy = sellDp[i - 1][j - 1] / prices[j];
                long rest = sellDp[i - 1][j - 1] % prices[j];
                if (buy == buyDp[i - 1][j]) {
                    rest = Math.max(rest, restDp[i - 1][j]);
                } else if (buy < buyDp[i - 1][j]) {
                    buy = buyDp[i - 1][j];
                    rest = restDp[i - 1][j];
                }
                if (buy == buyDp[i][j - 1]) {
                    rest = Math.max(rest, restDp[i][j - 1]);
                } else if (buy < buyDp[i][j - 1]) {
                    buy = buyDp[i][j - 1];
                    rest = restDp[i][j - 1];
                }
                buyDp[i][j] = buy;
                restDp[i][j] = rest;
            }
            for (int j = 1; j <= n + 1; j++) {
                sellDp[i][j] = Math.max(Math.max(sellDp[i - 1][j], sellDp[i][j - 1]), Math.max(sellDp[i][j], buyDp[i][j - 1] * prices[j] + restDp[i][j - 1]));
            }
        }
        System.out.println(sellDp[m][n + 1]);
    }
}
0