結果
| 問題 |
No.1536 仕切り直し
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-11-09 08:41:49 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,773 bytes |
| コンパイル時間 | 2,263 ms |
| コンパイル使用メモリ | 79,260 KB |
| 実行使用メモリ | 118,892 KB |
| 最終ジャッジ日時 | 2024-11-17 19:44:06 |
| 合計ジャッジ時間 | 23,017 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 4 TLE * 6 |
ソースコード
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 <= n + 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();
}
}
tenten