結果

問題 No.484 収穫
ユーザー tanzakutanzaku
提出日時 2017-02-09 23:34:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 664 ms / 3,000 ms
コード長 2,630 bytes
コンパイル時間 3,229 ms
コンパイル使用メモリ 79,240 KB
実行使用メモリ 183,960 KB
最終ジャッジ日時 2024-04-21 14:22:49
合計ジャッジ時間 12,249 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
36,476 KB
testcase_01 AC 44 ms
36,544 KB
testcase_02 AC 43 ms
36,708 KB
testcase_03 AC 44 ms
36,724 KB
testcase_04 AC 43 ms
36,708 KB
testcase_05 AC 44 ms
37,024 KB
testcase_06 AC 44 ms
36,560 KB
testcase_07 AC 46 ms
36,980 KB
testcase_08 AC 47 ms
36,704 KB
testcase_09 AC 60 ms
37,972 KB
testcase_10 AC 59 ms
38,088 KB
testcase_11 AC 72 ms
38,272 KB
testcase_12 AC 608 ms
180,364 KB
testcase_13 AC 617 ms
180,352 KB
testcase_14 AC 641 ms
180,340 KB
testcase_15 AC 646 ms
180,316 KB
testcase_16 AC 644 ms
180,108 KB
testcase_17 AC 653 ms
180,276 KB
testcase_18 AC 664 ms
183,960 KB
testcase_19 AC 638 ms
180,336 KB
testcase_20 AC 647 ms
180,344 KB
testcase_21 AC 618 ms
180,104 KB
testcase_22 AC 613 ms
180,348 KB
testcase_23 AC 620 ms
180,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class InputValidator implements Closeable {
	final InputStream in;
	
	final int NO_BUFFER = -2;
	int buffer;
	
	public InputValidator(final InputStream in) {
		this.in = in;
		buffer = NO_BUFFER;
	}
	
	int read() throws IOException {
		final int res = buffer == NO_BUFFER ? in.read() : buffer;
		buffer = NO_BUFFER;
		return res;
	}
	
	void unread(int ch) throws IOException {
		buffer = ch;
	}
	
	// [low, high]
	long nextLong(long low, long high) throws IOException {
		long val = 0;
		int ch = -1;
		while (true) {
			ch = read();
			if (!Character.isDigit(ch)) break;
			val = val * 10 + ch - '0';
			check(val <= high);
		}
		check(ch >= 0);
		check(val >= low);
		unread(ch);
		return val;
	}
	int nextInt(int low, int high) throws IOException { return (int)nextLong(low, high); }
	int[] nextInts(int n, int low, int high) throws IOException {
		int[] res = new int[n];
		for (int i = 0; i < n; i++) {
			res[i] = nextInt(low, high);
			if (i + 1 != n) space();
		}
		newLineOrEOF();
		return res;
	}
	
	void space() throws IOException { int ch = read(); check(ch == ' '); }
	void newLine() throws IOException {
		int ch = read();
		if (ch == '\r') ch = read();
		check(ch == '\n');
	}
	void newLineOrEOF() throws IOException { int ch = read(); check(ch == '\r' || ch == '\n' || ch < 0); }
	void check(boolean b) { if (!b) throw new RuntimeException(); }

	@Override
	public void close() throws IOException {
	}
}

public class _483_Validate {
	public static void main(String[] args) throws IOException {
		new _483_Validate().solve();
	}

	void solve() throws IOException {
		try (final InputValidator in = new InputValidator(System.in)) {
//		try (final Scanner in = new Scanner(System.in)) {
			int n = in.nextInt(1, 2000);
			in.newLine();
			A = in.nextInts(n, 0, 1_000_000_000);
			memo = new int[n][n][2];
			for (int[][] me : memo) for (int[] mo : me) Arrays.fill(mo, -1);
			int ans = INF;
			for (int i = 0; i < n; i++) {
				ans = Math.min(ans, rec(i, i, 0));
			}
			System.out.println(ans);
		}
	}
	
	int[] A;
	final int INF = 1<<30;
	int[][][] memo;
	int rec(int l, int r, int lr) {
		if (l < 0) return INF;
		if (r >= A.length) return INF;
		if (r - l + 1 == A.length) return A[lr == 0 ? l : r];
		if (memo[l][r][lr] != -1) return memo[l][r][lr];
		final int x = lr == 0 ? l : r;
		int ans = INF;
		ans = Math.min(ans, (rec(l - 1, r, 0) + x - l + 1));
		ans = Math.min(ans, (rec(l, r + 1, 1) + r - x + 1));
		ans = Math.max(A[x], ans);
		return memo[l][r][lr] = ans;
	}
	
	// for debug
	static void dump(Object... o) {
		System.err.println(Arrays.deepToString(o));
	}
}
0