結果

問題 No.1460 Max of Min
ユーザー akakimidoriakakimidori
提出日時 2021-04-02 03:10:56
言語 Rust
(1.77.0)
結果
AC  
実行時間 346 ms / 2,000 ms
コード長 2,639 bytes
コンパイル時間 1,587 ms
コンパイル使用メモリ 148,824 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-08-23 10:54:50
合計ジャッジ時間 14,144 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 42 ms
4,376 KB
testcase_04 AC 346 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 53 ms
4,380 KB
testcase_07 AC 309 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 294 ms
4,376 KB
testcase_11 AC 162 ms
4,376 KB
testcase_12 AC 24 ms
4,380 KB
testcase_13 AC 96 ms
4,380 KB
testcase_14 AC 49 ms
4,376 KB
testcase_15 AC 17 ms
4,376 KB
testcase_16 AC 65 ms
4,380 KB
testcase_17 AC 98 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 57 ms
4,376 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 9 ms
4,504 KB
testcase_22 AC 55 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 75 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 186 ms
4,376 KB
testcase_27 AC 30 ms
4,376 KB
testcase_28 AC 8 ms
4,380 KB
testcase_29 AC 98 ms
4,380 KB
testcase_30 AC 78 ms
4,376 KB
testcase_31 AC 36 ms
4,376 KB
testcase_32 AC 181 ms
4,380 KB
testcase_33 AC 9 ms
4,376 KB
testcase_34 AC 51 ms
4,380 KB
testcase_35 AC 18 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 65 ms
4,380 KB
testcase_38 AC 29 ms
4,376 KB
testcase_39 AC 37 ms
4,380 KB
testcase_40 AC 32 ms
4,380 KB
testcase_41 AC 16 ms
4,380 KB
testcase_42 AC 20 ms
4,384 KB
testcase_43 AC 52 ms
4,380 KB
testcase_44 AC 141 ms
4,384 KB
testcase_45 AC 4 ms
4,376 KB
testcase_46 AC 37 ms
4,380 KB
testcase_47 AC 34 ms
4,376 KB
testcase_48 AC 107 ms
4,376 KB
testcase_49 AC 79 ms
4,376 KB
testcase_50 AC 5 ms
4,380 KB
testcase_51 AC 78 ms
4,376 KB
testcase_52 AC 17 ms
4,508 KB
testcase_53 AC 61 ms
4,380 KB
testcase_54 AC 176 ms
4,384 KB
testcase_55 AC 178 ms
4,384 KB
testcase_56 AC 33 ms
4,500 KB
testcase_57 AC 165 ms
4,380 KB
testcase_58 AC 132 ms
4,380 KB
testcase_59 AC 108 ms
4,380 KB
testcase_60 AC 113 ms
4,380 KB
testcase_61 AC 215 ms
4,376 KB
testcase_62 AC 189 ms
4,384 KB
testcase_63 AC 1 ms
4,376 KB
testcase_64 AC 1 ms
4,376 KB
testcase_65 AC 138 ms
4,376 KB
testcase_66 AC 183 ms
4,376 KB
testcase_67 AC 23 ms
4,380 KB
testcase_68 AC 88 ms
4,380 KB
testcase_69 AC 153 ms
4,376 KB
testcase_70 AC 187 ms
4,500 KB
testcase_71 AC 110 ms
4,380 KB
testcase_72 AC 123 ms
4,380 KB
testcase_73 AC 196 ms
4,380 KB
testcase_74 AC 39 ms
4,376 KB
testcase_75 AC 36 ms
4,376 KB
testcase_76 AC 38 ms
4,376 KB
testcase_77 AC 37 ms
4,380 KB
testcase_78 AC 29 ms
4,380 KB
testcase_79 AC 35 ms
4,384 KB
testcase_80 AC 26 ms
4,380 KB
testcase_81 AC 29 ms
4,380 KB
testcase_82 AC 35 ms
4,380 KB
testcase_83 AC 26 ms
4,376 KB
testcase_84 AC 261 ms
4,380 KB
testcase_85 AC 256 ms
4,500 KB
testcase_86 AC 258 ms
4,380 KB
testcase_87 AC 265 ms
4,380 KB
testcase_88 AC 262 ms
4,384 KB
testcase_89 AC 260 ms
4,380 KB
testcase_90 AC 258 ms
4,380 KB
testcase_91 AC 267 ms
4,376 KB
testcase_92 AC 267 ms
4,380 KB
testcase_93 AC 255 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------

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

const INF: i64 = 1_000_000_000_000_000_000 + 1;

fn solve(n: usize, a: &[i64], b: &[i64]) -> i64 {
    if n < a.len() {
        return a[n];
    }
    let k = a.len();
    let mut ok = -INF;
    let mut ng = INF;
    while ng - ok > 1 {
        let mid = (ok + ng) / 2;
        let a = a.iter().map(|a| *a >= mid).collect::<Vec<_>>();
        let b = b.iter().map(|a| *a >= mid).collect::<Vec<_>>();
        let mut c = vec![false; 2 * k];
        c[..k].copy_from_slice(&a);
        for i in 0..k {
            c[i + k] = c[i..].iter().zip(b.iter()).any(|p| *p.0 && *p.1);
        }
        if n < 2 * k {
            if c[n] {
                ok = mid;
            } else {
                ng = mid;
            }
            continue;
        }
        if c[k..].iter().all(|p| !*p) {
            ng = mid;
            continue;
        }
        let len = k - b.iter().position(|c| *c).unwrap();
        let mut dp = vec![n + 1; len];
        for (i, c) in c[k..].iter().enumerate() {
            if *c {
                dp[i % len].chmin(k + i);
            }
        }
        let mut used = vec![false; len];
        for _ in 1..len {
            let x = (0..len).filter(|p| !used[*p]).min_by_key(|p| dp[*p]).unwrap();
            used[x] = true;
            let v = dp[x];
            for (i, c) in b.iter().enumerate() {
                if *c {
                    dp[(x + k - i) % len].chmin(v + k - i);
                }
            }
        }
        if dp[(n - k) % len] <= n {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    ok
}

fn run() {
    let (n, a, b) = read();
    let ans = solve(n, &a, &b);
    println!("{}", ans);
}

fn main() {
    run();
}
0