結果

問題 No.1460 Max of Min
ユーザー akakimidori
提出日時 2021-04-03 05:52:17
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 2,009 bytes
コンパイル時間 18,521 ms
コンパイル使用メモリ 375,168 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2025-03-28 08:59:32
合計ジャッジ時間 19,605 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 91
権限があれば一括ダウンロードができます

ソースコード

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