結果

問題 No.1460 Max of Min
ユーザー akakimidori
提出日時 2021-12-09 04:21:13
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 2,680 bytes
コンパイル時間 13,820 ms
コンパイル使用メモリ 396,796 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-16 15:01:31
合計ジャッジ時間 20,452 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 91
権限があれば一括ダウンロードができます

ソースコード

diff #

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

pub struct Kitamasa<T> {
    c: Vec<T>,
    tmp: std::cell::RefCell<Vec<T>>,
}

impl<T: SemiRing> Kitamasa<T> {
    pub fn new(c: Vec<T>) -> Self {
        assert!(!c.is_empty());
        Self {
            c: c,
            tmp: std::cell::RefCell::new(vec![]),
        }
    }
    pub fn normalize(&self, d: &mut Vec<T>) {
        let n = self.c.len();
        for i in (n..d.len()).rev() {
            let v = d.pop().unwrap();
            for (d, c) in d[(i - n)..].iter_mut().zip(&self.c) {
                *d = d.add(&v.mul(c));
            }
        }
    }
    pub fn next(&self, d: &mut Vec<T>) {
        d.insert(0, T::zero());
        self.normalize(d);
    }
    pub fn twice(&self, d: &mut Vec<T>) {
        assert!(!d.is_empty());
        let mut tmp = self.tmp.borrow_mut();
        tmp.clear();
        tmp.resize(d.len() * 2 - 1, T::zero());
        for (i, a) in d.iter().enumerate() {
            for (tmp, b) in tmp[i..].iter_mut().zip(d.iter()) {
                *tmp = tmp.add(&a.mul(b));
            }
        }
        std::mem::swap(&mut *tmp, d);
        drop(tmp);
        self.normalize(d);
    }
    pub fn kth_coefficient(&self, k: usize) -> Vec<T> {
        let mut t = vec![T::one()];
        if k > 0 {
            let p = (k + 1).next_power_of_two().trailing_zeros() - 1;
            for i in (0..=p).rev() {
                self.twice(&mut t);
                if k >> i & 1 == 1 {
                    self.next(&mut t);
                }
            }
        }
        t.resize(self.c.len(), T::zero());
        t
    }
}

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)
}

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

fn main() {
    let (n, a, b) = read();
    let solver = Kitamasa::new(b);
    let d = solver.kth_coefficient(n);
    let ans = a.into_iter().zip(d).fold(std::i64::MIN, |s, p| s.max(p.0.min(p.1)));
    println!("{}", ans);
}
0