結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー phsplsphspls
提出日時 2022-11-22 00:42:36
言語 Rust
(1.77.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,442 bytes
コンパイル時間 3,121 ms
コンパイル使用メモリ 173,260 KB
実行使用メモリ 15,700 KB
最終ジャッジ日時 2023-10-23 19:52:31
合計ジャッジ時間 5,601 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 0 ms
4,348 KB
testcase_02 AC 45 ms
4,348 KB
testcase_03 AC 9 ms
4,348 KB
testcase_04 AC 17 ms
4,348 KB
testcase_05 AC 72 ms
15,700 KB
testcase_06 AC 71 ms
15,700 KB
testcase_07 AC 70 ms
15,700 KB
testcase_08 AC 72 ms
15,700 KB
testcase_09 AC 78 ms
15,088 KB
testcase_10 AC 77 ms
15,088 KB
testcase_11 AC 78 ms
15,084 KB
testcase_12 AC 78 ms
15,084 KB
testcase_13 AC 78 ms
15,084 KB
testcase_14 AC 79 ms
15,088 KB
testcase_15 AC 78 ms
15,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn is_kadomatsu(a: &Vec<usize>) -> bool {
    a[0] != a[1] && a[1] != a[2] && a[2] != a[0]
    && (*a.iter().min().unwrap() == a[1] || *a.iter().max().unwrap() == a[1])
}

fn solve() -> usize {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut a = String::new();
    std::io::stdin().read_line(&mut a).ok();
    let a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let mut result = 0usize;
    for start in 0..3 {
        let mut dp = vec![vec![0usize; 2]; n+1];
        for i in start..n+start-2 {
            let aval = a[i];
            let target = vec![aval, a[(i+1)%n], a[(i+2)%n]];
            let kadoval = if is_kadomatsu(&target) { aval } else { 0 };
            dp[i+1][0] = dp[i][0];
            dp[i+1][1] = dp[i][0] + kadoval;
            if i >= 2 {
                dp[i+1][0] = dp[i+1][0].max(dp[i-2][1]);
                dp[i+1][1] = dp[i+1][1].max(dp[i-2][1] + kadoval)
            }
        }
        result = result.max(dp.iter().map(|v| *v.iter().max().unwrap()).max().unwrap());
    }
    result
}

fn main() {
    let mut t = String::new();
    std::io::stdin().read_line(&mut t).ok();
    let t: usize = t.trim().parse().unwrap();
    let mut result = Vec::with_capacity(t);
    for _ in 0..t {
        result.push(solve());
    }
    for &v in result.iter() {
        println!("{}", v);
    }
}
0