結果

問題 No.484 収穫
ユーザー koba-e964koba-e964
提出日時 2020-01-02 00:29:38
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 2,553 bytes
コンパイル時間 12,850 ms
コンパイル使用メモリ 396,164 KB
実行使用メモリ 220,928 KB
最終ジャッジ日時 2024-05-02 06:10:14
合計ジャッジ時間 18,821 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 0 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 4 ms
6,944 KB
testcase_10 AC 4 ms
6,944 KB
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 522 ms
220,928 KB
testcase_13 AC 521 ms
220,800 KB
testcase_14 AC 517 ms
220,928 KB
testcase_15 AC 514 ms
220,800 KB
testcase_16 AC 508 ms
220,800 KB
testcase_17 AC 514 ms
220,800 KB
testcase_18 AC 515 ms
220,800 KB
testcase_19 AC 510 ms
220,800 KB
testcase_20 AC 513 ms
220,800 KB
testcase_21 AC 514 ms
220,800 KB
testcase_22 AC 512 ms
220,800 KB
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::*;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    return ret;
}
fn get_word() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = std::string::String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}
fn parse<T: std::str::FromStr>(s: &str) -> T { s.parse::<T>().ok().unwrap() }

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { parse(&get_word()) }

const INF: i64 = 1 << 50;

fn solve(a: &[i64]) -> i64 {
    let n = a.len();
    if n == 1 {
        return a[0];
    }
    let mut dp = vec![vec![vec![INF; 2]; n + 1]; n];
    dp[1][n][0] = a[0];
    dp[0][n - 1][1] = a[n - 1];
    for s in (0 .. n - 1).rev() {
        for i in 0 .. min(n - s + 1, n) {
            // dp[i][i + s][0], goes to i - 1
            let mut res = INF;
            // i + s -> i - 1
            if i >= 1 && i + s < n {
                res = min(res, dp[i - 1][i + s][1] + s as i64 + 1);
            }
            // i - 2 -> i - 1
            if i >= 2 {
                res = min(res, dp[i - 1][i + s][0] + 1);
            }
            if i >= 1 {
                res = max(res, a[i - 1]);
            }
            dp[i][i + s][0] = res;
            // dp[i][i + s][1], goes to i + s
            let mut res = INF;
            // i - 1 -> i + s
            if i >= 1 && i + s + 1 <= n {
                res = min(res, dp[i][i + s + 1][0] + s as i64 + 1);
            }
            // i + s + 1 -> i + s
            if i + s + 1 <= n - 1 {
                res = min(res, dp[i][i + s + 1][1] + 1);
            }
            if i + s + 1 < n {
                res = max(res, a[i + s]);
            }
            dp[i][i + s][1] = res;
            //println!("dp[{}][{}] = {:?}", i, i + s, dp[i][i + s]);
        }
    }
    let mut mi = 1 << 58;
    for i in 0 .. n {
        mi = min(mi, min(dp[i][i][0], dp[i][i][1]));
    }
    mi
}


// Solved after reading the editorial
fn main() {
    let n = get();
    let a: Vec<i64> = (0..n).map(|_| get()).collect();
    println!("{}", solve(&a));
}
0