結果

問題 No.1607 Kth Maximum Card
ユーザー akakimidoriakakimidori
提出日時 2021-07-16 23:31:09
言語 Rust
(1.77.0)
結果
AC  
実行時間 506 ms / 3,500 ms
コード長 3,390 bytes
コンパイル時間 4,520 ms
コンパイル使用メモリ 154,684 KB
実行使用メモリ 20,208 KB
最終ジャッジ日時 2023-09-20 16:02:50
合計ジャッジ時間 7,603 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 261 ms
19,704 KB
testcase_09 AC 198 ms
16,996 KB
testcase_10 AC 277 ms
19,692 KB
testcase_11 AC 41 ms
6,244 KB
testcase_12 AC 173 ms
16,708 KB
testcase_13 AC 32 ms
5,428 KB
testcase_14 AC 38 ms
6,088 KB
testcase_15 AC 289 ms
17,880 KB
testcase_16 AC 29 ms
5,860 KB
testcase_17 AC 12 ms
4,380 KB
testcase_18 AC 120 ms
14,824 KB
testcase_19 AC 70 ms
9,600 KB
testcase_20 AC 113 ms
10,716 KB
testcase_21 AC 121 ms
13,720 KB
testcase_22 AC 506 ms
20,164 KB
testcase_23 AC 495 ms
20,208 KB
testcase_24 AC 68 ms
8,420 KB
testcase_25 AC 35 ms
5,776 KB
testcase_26 AC 52 ms
6,336 KB
testcase_27 AC 79 ms
8,596 KB
testcase_28 AC 61 ms
6,860 KB
testcase_29 AC 118 ms
14,492 KB
testcase_30 AC 337 ms
19,664 KB
testcase_31 AC 118 ms
11,400 KB
testcase_32 AC 59 ms
16,896 KB
testcase_33 AC 60 ms
16,864 KB
testcase_34 AC 55 ms
16,876 KB
testcase_35 AC 55 ms
16,872 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 ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

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

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------

use std::collections::*;

type Deque<T> = VecDeque<T>;

fn run() {
    input! {
        n: usize,
        m: usize,
        k: u32,
        e: [(usize1, usize1, u32); m],
    }
    let mut cnt = vec![0; n];
    for &(a, b, _) in e.iter() {
        cnt[a] += 1;
        cnt[b] += 1;
    }
    for i in 1..n {
        cnt[i] += cnt[i - 1];
    }
    let mut g = vec![(0, 0); 2 * m];
    let mut po = cnt.clone();
    cnt.insert(0, 0);
    for (a, b, c) in e {
        po[a] -= 1;
        g[po[a]] = (b, c);
        po[b] -= 1;
        g[po[b]] = (a, c);
    }
    let g = |v: usize| -> &[(usize, u32)] {
        &g[cnt[v]..cnt[v + 1]]
    };
    let mut dp = vec![k + 1; n];
    let mut deq = Deque::new();
    let mut valid = |m: u32| -> bool {
        dp.clear();
        deq.clear();
        dp.resize(n, k + 1);
        dp[0] = 0;
        deq.push_back(0);
        while let Some(v) = deq.pop_front() {
            let d = dp[v];
            for &(u, c) in g(v).iter() {
                if c < m && dp[u].chmin(d) {
                    deq.push_front(u);
                }
                if c >= m && dp[u].chmin(d + 1) {
                    deq.push_back(u);
                }
            }
        }
        dp[n - 1] < k
    };
    let mut ng = 0;
    let mut ok = 200001;
    while ok - ng > 1 {
        let mid = (ok + ng) / 2;
        if valid(mid) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    println!("{}", ng);
}

fn main() {
    run();
}
0