結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー fukafukatanifukafukatani
提出日時 2021-01-29 22:41:36
言語 Rust
(1.77.0)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 2,965 bytes
コンパイル時間 2,075 ms
コンパイル使用メモリ 152,556 KB
実行使用メモリ 8,120 KB
最終ジャッジ日時 2023-09-09 16:15:51
合計ジャッジ時間 4,198 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 38 ms
4,376 KB
testcase_03 AC 8 ms
4,376 KB
testcase_04 AC 14 ms
4,376 KB
testcase_05 AC 146 ms
7,932 KB
testcase_06 AC 146 ms
8,004 KB
testcase_07 AC 131 ms
8,120 KB
testcase_08 AC 146 ms
8,004 KB
testcase_09 AC 143 ms
7,596 KB
testcase_10 AC 143 ms
7,612 KB
testcase_11 AC 146 ms
7,484 KB
testcase_12 AC 145 ms
7,484 KB
testcase_13 AC 144 ms
7,600 KB
testcase_14 AC 147 ms
7,600 KB
testcase_15 AC 154 ms
7,484 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> Main.rs:20:9
   |
20 |     for i in 0..t {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let t = read::<usize>();
    for i in 0..t {
        solve();
    }
}

fn solve() {
    let n = read::<usize>();
    let a = read_vec::<i64>();
    let mut a = a.into_iter().collect::<VecDeque<_>>();
    let mut ret = 0;
    for _ in 0..3 {
        let mut dp = SegTree::new(n, 0, |a, b| max(a, b));
        for i in 2..n {
            let mut val = 0;
            if is_kadomatsu((a[i - 2], a[i - 1], a[i])) {
                val = a[i - 2];
            }
            dp.update(i, dp.query(0, i - 2) + val);
        }
        //debug!(*dp.iter().max().unwrap());
        ret = max(ret, dp.query(0, n));

        let t = a.pop_back().unwrap();
        a.push_front(t);
    }
    println!("{}", ret);
}

fn is_kadomatsu(a: (i64, i64, i64)) -> bool {
    if a.0 == a.2 {
        return false;
    }
    (a.0 < a.1 && a.1 > a.2) || (a.0 > a.1 && a.1 < a.2)
}

#[derive(Clone)]
struct SegTree<T, F>
where
    F: Fn(T, T) -> T,
    T: std::clone::Clone + std::marker::Copy,
{
    n: usize,
    dat: Vec<T>,
    init: T,
    functor: F,
}

impl<T, F> SegTree<T, F>
where
    F: Fn(T, T) -> T,
    T: std::clone::Clone + std::marker::Copy,
{
    fn new(n: usize, init: T, f: F) -> SegTree<T, F> {
        let mut m = 1;
        // For simplicity, we use 2 ** n sized SegTree.
        while m < n {
            m *= 2;
        }
        SegTree {
            n: m,
            dat: vec![init; 2 * m - 1],
            init: init,
            functor: f,
        }
    }

    // dat[k] = a;
    fn update(&mut self, k: usize, a: T) {
        let mut k = k;
        k += self.n - 1;
        self.dat[k] = a;
        while k > 0 {
            k = (k - 1) / 2;
            self.dat[k] = (self.functor)(self.dat[k * 2 + 1], self.dat[k * 2 + 2]);
        }
    }

    // [a, b)
    fn query(&self, a: usize, b: usize) -> T {
        self.query_inner(a, b, 0, 0, self.n)
    }

    fn query_inner(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T {
        if r <= a || b <= l {
            return self.init;
        }
        if a <= l && r <= b {
            return self.dat[k];
        }

        let vl = self.query_inner(a, b, k * 2 + 1, l, (l + r) / 2);
        let vr = self.query_inner(a, b, k * 2 + 2, (l + r) / 2, r);
        (self.functor)(vl, vr)
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
0