結果

問題 No.484 収穫
コンテスト
ユーザー nebukuro09
提出日時 2017-05-10 17:48:27
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 1,077 ms / 3,000 ms
コード長 1,338 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 573 ms
コンパイル使用メモリ 98,720 KB
実行使用メモリ 134,824 KB
最終ジャッジ日時 2026-03-05 14:34:24
合計ジャッジ時間 13,198 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    immutable int INF = 2 * 10^^9;
    
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int);

    if (N == 1) {
        writeln(A[0]);
        return;
    }

    /*
      dp[l][r][left or right]: 
      未訪問の区間がl~rで、最後に訪問済のノードが l-1 or r+1 のときの最小コスト
     */
    auto dp = new int[][][](N, N, 2);
    foreach (i; 0..N) foreach (j; 0..N) fill(dp[i][j], INF);
    dp[1][N-1][0] = A[0];
    dp[0][N-2][1] = A[N-1];

    foreach (len; iota(N-1, 1, -1)) {
        foreach (i; 0..N-len+1) {
            int j = i + len - 1;
            int ltol = max(dp[i][j][0] + 1, A[i]);
            int rtol = max(dp[i][j][1] + j - i, A[i]);
            int ltor = max(dp[i][j][0] + j - i, A[j]);
            int rtor = max(dp[i][j][1] + 1, A[j]);
            dp[i+1][j][0] = min(dp[i+1][j][0], min(ltol, rtol));
            dp[i][j-1][1] = min(dp[i][j-1][1], min(ltor, rtor));
        }
    }

    
    int ans = INF;
    foreach (i; 0..N) {
        ans = min(ans, max(dp[i][i][0]+1, A[i]));
        ans = min(ans, max(dp[i][i][1]+1, A[i]));
    }

    ans.writeln;
}
0