結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー phsplsphspls
提出日時 2022-11-20 20:57:13
言語 Rust
(1.77.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 1,395 bytes
コンパイル時間 1,136 ms
コンパイル使用メモリ 177,944 KB
実行使用メモリ 15,700 KB
最終ジャッジ日時 2023-10-21 17:13:48
合計ジャッジ時間 2,942 ms
ジャッジサーバーID
(参考情報)
judge9 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

fn solve() -> isize {
    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<isize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let mut result = 0isize;
    for start in 0..3 {
        let mut dp = vec![vec![0isize; 2]; n+1];
        for i in 1+start..=n+start-2 {
            let aval = a[(i-1) % n];
            dp[i][0] = dp[i-1][0];
            dp[i][1] = dp[i-1][0];
            dp[i][0] = dp[i][0].max(if i<3 { 0 } else { dp[i-3][1] });
            let vals = vec![aval, a[i % n], a[(i+1) % n]];
            dp[i][1] = dp[i][1].max(if i<3 { 0 } else { dp[i-3][1] }) + if is_kadomatsu(&vals) { aval } else { 0 };
        }
        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