結果

問題 No.127 門松もどき
ユーザー koba-e964koba-e964
提出日時 2017-02-07 21:51:58
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,234 ms / 5,000 ms
コード長 3,386 bytes
コンパイル時間 3,516 ms
コンパイル使用メモリ 147,372 KB
実行使用メモリ 495,980 KB
最終ジャッジ日時 2023-08-25 15:29:24
合計ジャッジ時間 15,368 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1,234 ms
495,804 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 66 ms
35,444 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 556 ms
262,256 KB
testcase_13 AC 754 ms
350,920 KB
testcase_14 AC 699 ms
320,572 KB
testcase_15 AC 1,009 ms
444,784 KB
testcase_16 AC 665 ms
310,076 KB
testcase_17 AC 728 ms
359,916 KB
testcase_18 AC 611 ms
297,864 KB
testcase_19 AC 333 ms
169,772 KB
testcase_20 AC 338 ms
174,884 KB
testcase_21 AC 145 ms
81,144 KB
testcase_22 AC 1,149 ms
495,980 KB
testcase_23 AC 1,067 ms
495,980 KB
testcase_24 AC 881 ms
380,232 KB
testcase_25 AC 1,077 ms
449,704 KB
testcase_26 AC 454 ms
206,360 KB
権限があれば一括ダウンロードができます

ソースコード

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.is_err() || res.ok().unwrap() == 0 || u8b[0] <= ' ' as u8 {
                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()) }

/**
 * Sparse Table.
 * BiOp should be the type of a binary operator which is
 * associative, commutative and idempotent.
 * (For example, both min and gcd satisfy these properties.)
 * Verified by: AtCoder CODE FESTIVAL 2016 Tournament Round 3 (Parallel) B
 * (http://cf16-tournament-round3-open.contest.atcoder.jp/submissions/1026294)
 */
struct SparseTable<T, BiOp> {
    biop: BiOp,
    st: Vec<Vec<T>>,
}

impl<T, BiOp> SparseTable<T, BiOp>
    where BiOp: Fn(T, T) -> T,
          T: Copy {
    pub fn new(ary: &[T], biop: BiOp) -> Self {
        let n = ary.len();
        let mut h = 1;
        while 1 << h < n {
            h += 1;
        }
        let mut st: Vec<Vec<T>> = vec![Vec::from(ary); h + 1];
        for i in 0 .. n {
            st[0][i] = ary[i];
        }
        for b in 1 .. (h + 1) {
            if n + 1 < 1 << b {
                break;
            }
            for i in 0 .. (n + 1 - (1 << b)) {
                let next_idx = (1 << (b - 1)) + i;
                st[b][i] = biop(st[b - 1][i], st[b - 1][next_idx]);
            }
        }
        SparseTable {biop: biop, st: st}
    }
    fn top_bit(t: usize) -> usize {
        let mut h = 0;
        while 1 << h <= t {
            h += 1;
        }
        h - 1
    }
    pub fn query(&self, f: usize, s: usize) -> T {
        assert!(f <= s);
        let b = Self::top_bit(s + 1 - f);
        let endpoint = s + 1 - (1 << b);
        (self.biop)(self.st[b][f], self.st[b][endpoint])
    }
}


fn solve(a: &[(i32, usize)]) -> i32 {
    let n = a.len();
    let mut dp = vec![vec![0i32; n]; n];
    let mut dp_sparse = Vec::<SparseTable<i32, _>>::new();
    for i in 0 .. n {
        dp[i][a[i].1] = 1;
        for j in 0 .. i {
            if a[j].0 == a[i].0 { continue; }
            // >= a[j].1
            let mut ret = 1;
            if a[i].1 > a[j].1 {
                ret = max(ret, dp_sparse[j].query(a[j].1, a[i].1 - 1) + 1);
            } else {
                // <= a[j].1
                ret = max(ret, dp_sparse[j].query(a[i].1 + 1, a[j].1) + 1);
            }
            dp[i][a[j].1] = ret;
        }
        dp_sparse.push(SparseTable::new(&dp[i], |x, y| max(x, y)));
    }
    *dp.iter().map(|w| w.iter().max().unwrap()).max().unwrap()
}

fn main() {
    let n = get();
    let mut a: Vec<(i32, usize)> = (0 .. n).map(|i| (get(), i)).collect();
    a.sort();
    a.reverse();
    let t2 = solve(&a);
    println!("{}", t2);
}
0