結果

問題 No.1460 Max of Min
ユーザー akakimidoriakakimidori
提出日時 2021-04-03 05:52:17
言語 Rust
(1.77.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 2,009 bytes
コンパイル時間 1,725 ms
コンパイル使用メモリ 150,032 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-25 20:03:56
合計ジャッジ時間 8,891 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 151 ms
4,380 KB
testcase_04 AC 150 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 150 ms
4,376 KB
testcase_07 AC 152 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 167 ms
4,380 KB
testcase_11 AC 150 ms
4,380 KB
testcase_12 AC 14 ms
4,380 KB
testcase_13 AC 58 ms
4,380 KB
testcase_14 AC 21 ms
4,380 KB
testcase_15 AC 10 ms
4,380 KB
testcase_16 AC 49 ms
4,380 KB
testcase_17 AC 27 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 29 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 5 ms
4,380 KB
testcase_22 AC 30 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 32 ms
4,384 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 60 ms
4,376 KB
testcase_27 AC 11 ms
4,380 KB
testcase_28 AC 4 ms
4,380 KB
testcase_29 AC 32 ms
4,380 KB
testcase_30 AC 46 ms
4,380 KB
testcase_31 AC 28 ms
4,388 KB
testcase_32 AC 9 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 6 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 6 ms
4,380 KB
testcase_38 AC 4 ms
4,380 KB
testcase_39 AC 4 ms
4,380 KB
testcase_40 AC 4 ms
4,380 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 13 ms
4,380 KB
testcase_44 AC 24 ms
4,380 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 9 ms
4,376 KB
testcase_47 AC 12 ms
4,380 KB
testcase_48 AC 27 ms
4,380 KB
testcase_49 AC 19 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 13 ms
4,380 KB
testcase_52 AC 4 ms
4,380 KB
testcase_53 AC 5 ms
4,376 KB
testcase_54 AC 6 ms
4,376 KB
testcase_55 AC 26 ms
4,380 KB
testcase_56 AC 6 ms
4,384 KB
testcase_57 AC 23 ms
4,376 KB
testcase_58 AC 151 ms
4,380 KB
testcase_59 AC 17 ms
4,376 KB
testcase_60 AC 20 ms
4,380 KB
testcase_61 AC 7 ms
4,376 KB
testcase_62 AC 7 ms
4,380 KB
testcase_63 AC 1 ms
4,380 KB
testcase_64 AC 2 ms
4,376 KB
testcase_65 AC 31 ms
4,380 KB
testcase_66 AC 7 ms
4,384 KB
testcase_67 AC 4 ms
4,380 KB
testcase_68 AC 22 ms
4,376 KB
testcase_69 AC 19 ms
4,376 KB
testcase_70 AC 12 ms
4,380 KB
testcase_71 AC 29 ms
4,380 KB
testcase_72 AC 6 ms
4,376 KB
testcase_73 AC 6 ms
4,380 KB
testcase_74 AC 6 ms
4,380 KB
testcase_75 AC 5 ms
4,380 KB
testcase_76 AC 5 ms
4,384 KB
testcase_77 AC 6 ms
4,376 KB
testcase_78 AC 3 ms
4,384 KB
testcase_79 AC 6 ms
4,376 KB
testcase_80 AC 4 ms
4,380 KB
testcase_81 AC 4 ms
4,376 KB
testcase_82 AC 4 ms
4,384 KB
testcase_83 AC 4 ms
4,376 KB
testcase_84 AC 152 ms
4,380 KB
testcase_85 AC 148 ms
4,376 KB
testcase_86 AC 153 ms
4,380 KB
testcase_87 AC 143 ms
4,384 KB
testcase_88 AC 148 ms
4,380 KB
testcase_89 AC 152 ms
4,384 KB
testcase_90 AC 141 ms
4,380 KB
testcase_91 AC 145 ms
4,380 KB
testcase_92 AC 167 ms
4,380 KB
testcase_93 AC 156 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn read() -> (usize, Vec<i64>, Vec<i64>) {
    let mut s = String::new();
    use std::io::Read;
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let mut next = || it.next().unwrap().parse::<i64>().unwrap();
    let k = next() as usize;
    let n = next() as usize;
    let a = (0..k).map(|_| next()).collect::<Vec<_>>();
    let b = (0..k).map(|_| next()).collect::<Vec<_>>();
    (n, a, b)
}

pub trait SemiRing {
    fn add(&self, rhs: &Self) -> Self;
    fn mul(&self, rhs: &Self) -> Self;
    fn zero() -> Self;
    fn one() -> Self;
}

pub fn kitamasa<T>(a: &[T], b: &[T], mut n: usize) -> T
where
    T: Clone + SemiRing,
{
    assert!(a.len() == b.len() && a.len() > 0);
    let k = a.len();
    let calc = |x: &[T], y: &[T]| -> Vec<T> {
        let mut z = vec![T::zero(); x.len() + y.len() - 1];
        for (i, x) in x.iter().enumerate() {
            for (z, y) in z[i..].iter_mut().zip(y.iter()) {
                *z = x.mul(y).add(z);
            }
        }
        for i in (k..z.len()).rev() {
            let p = z.pop().unwrap();
            for (z, b) in z[(i - k)..].iter_mut().zip(b.iter()) {
                *z = p.mul(&b).add(z);
            }
        }
        z
    };
    let mut t = vec![T::zero()];
    let mut s = vec![T::zero(); 2];
    t[0] = T::one();
    s[1] = T::one();
    while n > 0 {
        if n & 1 == 1 {
            t = calc(&t, &s);
        }
        s = calc(&s, &s);
        n >>= 1;
    }
    let mut ans = T::zero();
    for (t, a) in t.iter().zip(a.iter()) {
        ans = t.mul(a).add(&ans);
    }
    ans
}

impl SemiRing for i64 {
    fn add(&self, rhs: &Self) -> Self {
        std::cmp::max(*self, *rhs)
    }
    fn mul(&self, rhs: &Self) -> Self {
        std::cmp::min(*self, *rhs)
    }
    fn zero() -> Self {
        std::i64::MIN
    }
    fn one() -> Self {
        std::i64::MAX
    }
}

fn main() {
    let (n, a, b) = read();
    let ans = kitamasa(&a, &b, n);
    println!("{}", ans);
}
0