結果

問題 No.484 収穫
ユーザー zimphazimpha
提出日時 2017-03-30 21:41:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 274 ms / 3,000 ms
コード長 3,551 bytes
コンパイル時間 2,029 ms
コンパイル使用メモリ 77,948 KB
実行使用メモリ 122,788 KB
最終ジャッジ日時 2024-04-21 14:30:26
合計ジャッジ時間 6,775 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
36,992 KB
testcase_01 AC 46 ms
37,056 KB
testcase_02 AC 48 ms
37,052 KB
testcase_03 AC 45 ms
37,016 KB
testcase_04 AC 47 ms
37,304 KB
testcase_05 AC 46 ms
37,232 KB
testcase_06 AC 46 ms
37,032 KB
testcase_07 AC 48 ms
37,040 KB
testcase_08 AC 47 ms
37,012 KB
testcase_09 AC 56 ms
38,164 KB
testcase_10 AC 60 ms
38,472 KB
testcase_11 AC 58 ms
37,924 KB
testcase_12 AC 270 ms
122,780 KB
testcase_13 AC 265 ms
122,720 KB
testcase_14 AC 262 ms
122,472 KB
testcase_15 AC 265 ms
122,788 KB
testcase_16 AC 274 ms
122,604 KB
testcase_17 AC 270 ms
122,472 KB
testcase_18 AC 261 ms
122,328 KB
testcase_19 AC 271 ms
122,720 KB
testcase_20 AC 271 ms
122,464 KB
testcase_21 AC 268 ms
122,604 KB
testcase_22 AC 265 ms
122,664 KB
testcase_23 AC 267 ms
122,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
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[2][][];
            dp[0] = new long[n][];
            dp[1] = new long[n][];
            for (int i = 0; i < n; i++) {
                dp[0][i] = new long[n];
                dp[1][i] = new long[n];
                Arrays.fill(dp[0][i], -1);
                Arrays.fill(dp[1][i], -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[s][l][r] >= 0) return dp[s][l][r];
            long ret = 1l << 60;
            if (l == 0 && r == A.length - 1) {
                if (s == 0) dp[s][l][r] = A[l];
                else dp[s][l][r] = A[r];
                return dp[s][l][r];
            }
            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[s][l][r] = 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;
        }

    }
}

0