結果
| 問題 |
No.484 収穫
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-03-30 21:36:55 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 772 ms / 3,000 ms |
| コード長 | 3,489 bytes |
| コンパイル時間 | 2,653 ms |
| コンパイル使用メモリ | 77,700 KB |
| 実行使用メモリ | 227,332 KB |
| 最終ジャッジ日時 | 2024-10-13 13:15:42 |
| 合計ジャッジ時間 | 10,228 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 |
ソースコード
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Chiaki.Hoshinomori
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task484 solver = new Task484();
solver.solve(1, in, out);
out.close();
}
static class Task484 {
long[][][] dp;
long[] A;
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
A = in.nextLongArray(n);
dp = new long[n][][];
for (int i = 0; i < n; i++) {
dp[i] = new long[n][];
for (int j = 0; j < n; j++) {
dp[i][j] = new long[2];
dp[i][j][0] = dp[i][j][1] = -1;
}
}
long ret = 1l << 60;
for (int i = 0; i < n; i++) {
ret = Math.min(ret, solve(i, i, 0));
}
out.println(ret);
}
private long solve(int l, int r, int s) {
if (dp[l][r][s] >= 0) return dp[l][r][s];
long ret = 1l << 60;
if (l == 0 && r == A.length - 1) {
if (s == 0) dp[l][r][s] = A[l];
else dp[l][r][s] = A[r];
return dp[l][r][s];
}
if (s == 0) {
if (l > 0) {
ret = Math.min(ret, solve(l - 1, r, 0) + 1);
}
if (r + 1 < A.length) {
ret = Math.min(ret, solve(l, r + 1, 1) + r - l + 1);
}
ret = Math.max(ret, A[l]);
} else {
if (l > 0) {
ret = Math.min(ret, solve(l - 1, r, 0) + r - l + 1);
}
if (r + 1 < A.length) {
ret = Math.min(ret, solve(l, r + 1, 1) + 1);
}
ret = Math.max(ret, A[r]);
}
return dp[l][r][s] = ret;
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] nextLongArray(int n) {
long[] r = new long[n];
for (int i = 0; i < n; i++) {
r[i] = nextLong();
}
return r;
}
}
}