結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー phsplsphspls
提出日時 2022-11-20 20:15:58
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,375 bytes
コンパイル時間 2,293 ms
コンパイル使用メモリ 178,304 KB
実行使用メモリ 15,708 KB
最終ジャッジ日時 2023-10-21 16:47:19
合計ジャッジ時間 4,852 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 55 ms
15,708 KB
testcase_06 WA -
testcase_07 AC 54 ms
15,708 KB
testcase_08 AC 54 ms
15,708 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 58 ms
15,092 KB
testcase_13 WA -
testcase_14 AC 58 ms
15,096 KB
testcase_15 WA -
権限があれば一括ダウンロードができます

ソースコード

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