結果

問題 No.1460 Max of Min
ユーザー koba-e964koba-e964
提出日時 2022-01-04 01:14:34
言語 Rust
(1.77.0)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 2,284 bytes
コンパイル時間 808 ms
コンパイル使用メモリ 172,876 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 15:43:38
合計ジャッジ時間 9,854 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 220 ms
5,376 KB
testcase_04 AC 219 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 222 ms
5,376 KB
testcase_07 AC 226 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 262 ms
5,376 KB
testcase_11 AC 223 ms
5,376 KB
testcase_12 AC 26 ms
5,376 KB
testcase_13 AC 113 ms
5,376 KB
testcase_14 AC 38 ms
5,376 KB
testcase_15 AC 18 ms
5,376 KB
testcase_16 AC 103 ms
5,376 KB
testcase_17 AC 55 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 52 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 8 ms
5,376 KB
testcase_22 AC 57 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 65 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 115 ms
5,376 KB
testcase_27 AC 19 ms
5,376 KB
testcase_28 AC 7 ms
5,376 KB
testcase_29 AC 63 ms
5,376 KB
testcase_30 AC 98 ms
5,376 KB
testcase_31 AC 53 ms
5,376 KB
testcase_32 AC 40 ms
5,376 KB
testcase_33 AC 12 ms
5,376 KB
testcase_34 AC 24 ms
5,376 KB
testcase_35 AC 8 ms
5,376 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 25 ms
5,376 KB
testcase_38 AC 34 ms
5,376 KB
testcase_39 AC 16 ms
5,376 KB
testcase_40 AC 12 ms
5,376 KB
testcase_41 AC 9 ms
5,376 KB
testcase_42 AC 5 ms
5,376 KB
testcase_43 AC 36 ms
5,376 KB
testcase_44 AC 61 ms
5,376 KB
testcase_45 AC 2 ms
5,376 KB
testcase_46 AC 24 ms
5,376 KB
testcase_47 AC 34 ms
5,376 KB
testcase_48 AC 66 ms
5,376 KB
testcase_49 AC 43 ms
5,376 KB
testcase_50 AC 2 ms
5,376 KB
testcase_51 AC 32 ms
5,376 KB
testcase_52 AC 8 ms
5,376 KB
testcase_53 AC 28 ms
5,376 KB
testcase_54 AC 51 ms
5,376 KB
testcase_55 AC 72 ms
5,376 KB
testcase_56 AC 48 ms
5,376 KB
testcase_57 AC 67 ms
5,376 KB
testcase_58 AC 235 ms
5,376 KB
testcase_59 AC 72 ms
5,376 KB
testcase_60 AC 58 ms
5,376 KB
testcase_61 AC 47 ms
5,376 KB
testcase_62 AC 49 ms
5,376 KB
testcase_63 AC 31 ms
5,376 KB
testcase_64 AC 36 ms
5,376 KB
testcase_65 AC 87 ms
5,376 KB
testcase_66 AC 49 ms
5,376 KB
testcase_67 AC 38 ms
5,376 KB
testcase_68 AC 69 ms
5,376 KB
testcase_69 AC 72 ms
5,376 KB
testcase_70 AC 59 ms
5,376 KB
testcase_71 AC 73 ms
5,376 KB
testcase_72 AC 46 ms
5,376 KB
testcase_73 AC 46 ms
5,376 KB
testcase_74 AC 47 ms
5,376 KB
testcase_75 AC 45 ms
5,376 KB
testcase_76 AC 43 ms
5,376 KB
testcase_77 AC 53 ms
5,376 KB
testcase_78 AC 41 ms
5,376 KB
testcase_79 AC 55 ms
5,376 KB
testcase_80 AC 49 ms
5,376 KB
testcase_81 AC 41 ms
5,376 KB
testcase_82 AC 47 ms
5,376 KB
testcase_83 AC 46 ms
5,376 KB
testcase_84 AC 236 ms
5,376 KB
testcase_85 AC 229 ms
5,376 KB
testcase_86 AC 230 ms
5,376 KB
testcase_87 AC 222 ms
5,376 KB
testcase_88 AC 240 ms
5,376 KB
testcase_89 AC 238 ms
5,376 KB
testcase_90 AC 222 ms
5,376 KB
testcase_91 AC 217 ms
5,376 KB
testcase_92 AC 253 ms
5,376 KB
testcase_93 AC 231 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

const INF: i64 = 1 << 60;

fn mul(a1: &[i64], a2: &[i64], b: &[i64]) -> Vec<i64> {
    let k = a1.len();
    let mut out = vec![-INF; 2 * k];
    for i in 0..k {
        for j in 0..k {
            out[i + j] = max(out[i + j], min(a1[i], a2[j]));
        }
    }
    for i in (k..2 * k).rev() {
        for j in 0..k {
            out[i - k + j] = max(out[i - k + j], min(b[j], out[i]));
        }
    }
    out.truncate(k);
    out
}

fn pow(a: &[i64], mut e: i64, b: &[i64]) -> Vec<i64> {
    let k = a.len();
    let mut prod = vec![-INF; k];
    prod[0] = INF;
    let mut cur = a.to_vec();
    while e > 0 {
        if e % 2 == 1 {
            prod = mul(&prod, &cur, b);
        }
        cur = mul(&cur, &cur, b);
        e /= 2;
    }
    prod
}

// https://yukicoder.me/problems/no/1460 (3.5)
// (max, min)-半環上の行列累乗で O(K^3 log N)。Kitamasa 法が使えれば O(K^2 log N)。
// Tags: tropical-semiring, matrix-exponentiation-on-semiring, kitamasa
fn main() {
    input! {
        k: usize, n: i64,
        a: [i64; k],
        b: [i64; k],
    }
    let mut x = vec![-INF; k];
    if k > 1 {
        x[1] = INF;
    } else {
        x[0] = b[0];
    }
    let s = pow(&x, n, &b);
    let mut ans = -INF;
    for i in 0..k {
        ans = max(ans, min(s[i], a[i]));
    }
    println!("{}", ans);
}
0