結果

問題 No.992 最長増加部分列の数え上げ
ユーザー akakimidoriakakimidori
提出日時 2020-02-14 23:40:11
言語 Rust
(1.77.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 1,772 bytes
コンパイル時間 1,922 ms
コンパイル使用メモリ 169,088 KB
実行使用メモリ 9,572 KB
最終ジャッジ日時 2024-04-16 03:01:02
合計ジャッジ時間 5,104 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 29 ms
5,376 KB
testcase_05 AC 21 ms
5,376 KB
testcase_06 AC 37 ms
5,376 KB
testcase_07 AC 25 ms
5,376 KB
testcase_08 AC 14 ms
5,376 KB
testcase_09 AC 26 ms
5,376 KB
testcase_10 AC 37 ms
5,376 KB
testcase_11 AC 46 ms
6,408 KB
testcase_12 AC 9 ms
5,376 KB
testcase_13 AC 24 ms
5,376 KB
testcase_14 AC 25 ms
5,376 KB
testcase_15 AC 9 ms
5,376 KB
testcase_16 AC 75 ms
8,780 KB
testcase_17 AC 13 ms
5,376 KB
testcase_18 AC 24 ms
5,376 KB
testcase_19 AC 46 ms
6,372 KB
testcase_20 AC 83 ms
9,472 KB
testcase_21 AC 82 ms
9,308 KB
testcase_22 AC 81 ms
9,420 KB
testcase_23 AC 81 ms
9,440 KB
testcase_24 AC 82 ms
9,424 KB
testcase_25 AC 82 ms
9,440 KB
testcase_26 AC 82 ms
9,436 KB
testcase_27 AC 86 ms
9,572 KB
testcase_28 AC 84 ms
9,436 KB
testcase_29 AC 85 ms
9,440 KB
testcase_30 AC 59 ms
9,160 KB
testcase_31 AC 58 ms
9,200 KB
testcase_32 AC 59 ms
9,196 KB
testcase_33 AC 58 ms
9,000 KB
testcase_34 AC 57 ms
9,192 KB
testcase_35 AC 47 ms
9,276 KB
testcase_36 AC 48 ms
9,176 KB
testcase_37 AC 48 ms
9,192 KB
testcase_38 AC 48 ms
9,292 KB
testcase_39 AC 48 ms
9,192 KB
testcase_40 AC 61 ms
8,884 KB
testcase_41 AC 59 ms
8,880 KB
testcase_42 AC 59 ms
8,868 KB
testcase_43 AC 59 ms
9,008 KB
testcase_44 AC 62 ms
9,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;
use std::cmp::Ordering;
use std::ops::*;

const MOD: u32 = 1_000_000_007;

#[derive(Clone, Copy)]
struct ModInt(u32);

impl Add for ModInt {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        let v = self.0 + rhs.0;
        ModInt(if v >= MOD {
            v - MOD
        } else {
            v
        })
    }
}

#[derive(Clone, Copy)]
struct MaxCnt(usize, ModInt);

impl Add for MaxCnt {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        match self.0.cmp(&rhs.0) {
            Ordering::Less => rhs,
            Ordering::Greater => self,
            Ordering::Equal => MaxCnt(self.0, self.1 + rhs.1),
        }
    }
}

fn add(bit: &mut [MaxCnt], x: usize, v: MaxCnt) {
    let mut i = x;
    while i < bit.len() {
        bit[i] = bit[i] + v;
        i += i & (!i + 1);
    }
}

fn sum(bit: &[MaxCnt], x: usize) -> MaxCnt {
    let mut ans = MaxCnt(0, ModInt(1));
    let mut i = x;
    while i > 0 {
        ans = ans + bit[i];
        i -= i & (!i + 1);
    }
    ans
}

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let a: Vec<i32> = it.map(|s| s.parse().unwrap()).collect();
    assert!(a.len() == n);
    let mut b = a.clone();
    b.push(-1_000_000_000 - 1);
    b.sort();
    b.dedup();
    let a: Vec<usize> = a.into_iter().map(|a| b.binary_search(&a).unwrap()).collect();
    let mut bit = vec![MaxCnt(0, ModInt(0)); b.len()];
    for a in a {
        let s = sum(&bit, a - 1);
        add(&mut bit, a, MaxCnt(s.0 + 1, s.1));
    }
    let ans = sum(&bit, b.len() - 1).1;
    println!("{}", ans.0);
}

fn main() {
    run();
}
0