結果

問題 No.1536 仕切り直し
ユーザー tentententen
提出日時 2021-11-09 08:40:25
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,759 bytes
コンパイル時間 2,595 ms
コンパイル使用メモリ 78,632 KB
実行使用メモリ 69,428 KB
最終ジャッジ日時 2024-04-28 22:41:27
合計ジャッジ時間 6,681 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
57,372 KB
testcase_01 AC 55 ms
50,396 KB
testcase_02 AC 55 ms
50,328 KB
testcase_03 AC 217 ms
52,656 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        long[] values = new long[n + 1];
        for (int i = 1; i <= n; i++) {
            values[i] = sc.nextInt() + values[i - 1];
        }
        long[][] dp = new long[n + 1][m + 2];
        int[][] lasts = new int[n + 1][m + 2];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m + 1; j++) {
                dp[i][j] = Long.MIN_VALUE;
                for (int k = i - 1; k >= 0; k--) {
                    if (j % 2 == 0) {
                        if (dp[i][j] < dp[k][j - 1] - values[i] + values[k]) {
                            dp[i][j] = dp[k][j - 1] - values[i] + values[k];
                            lasts[i][j] = k;
                        }
                    } else {
                        if (dp[i][j] < dp[k][j - 1] + values[i] - values[k]) {
                            dp[i][j] = dp[k][j - 1] + values[i] - values[k];
                            lasts[i][j] = k;
                        }
                    }
                }
            }
        }
        long ans = Long.MIN_VALUE;
        int idx = 0;
        for (int i = 0; i < m + 2; i++) {
            if (ans < dp[n][i]) {
                ans = dp[n][i];
                idx = i;
            }
        }
        ArrayDeque<Integer> list = new ArrayDeque<>();
        int current = n;
        while (idx > 0) {
            list.push(lasts[current][idx]);
            current = lasts[current][idx];
            idx--;
        }
        list.pop();
        int count = m - list.size();
        StringBuilder sb = new StringBuilder();
        while (list.size() > 0) {
            sb.append(list.pop()).append("\n");
        }
        for (int i = 0; i < count; i++) {
            sb.append(n).append("\n");
        }
        System.out.print(sb);
    }
    
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0