結果
問題 | No.1950 片道きゃっちぼーる |
ユーザー | akakimidori |
提出日時 | 2022-05-20 22:15:40 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 123 ms / 3,000 ms |
コード長 | 5,426 bytes |
コンパイル時間 | 13,139 ms |
コンパイル使用メモリ | 383,728 KB |
実行使用メモリ | 25,856 KB |
最終ジャッジ日時 | 2024-09-20 08:27:41 |
合計ジャッジ時間 | 16,411 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,812 KB |
testcase_03 | AC | 68 ms
22,888 KB |
testcase_04 | AC | 69 ms
17,712 KB |
testcase_05 | AC | 0 ms
6,944 KB |
testcase_06 | AC | 62 ms
25,856 KB |
testcase_07 | AC | 61 ms
14,636 KB |
testcase_08 | AC | 68 ms
15,056 KB |
testcase_09 | AC | 69 ms
20,264 KB |
testcase_10 | AC | 73 ms
18,920 KB |
testcase_11 | AC | 67 ms
18,304 KB |
testcase_12 | AC | 70 ms
19,052 KB |
testcase_13 | AC | 88 ms
19,324 KB |
testcase_14 | AC | 84 ms
20,556 KB |
testcase_15 | AC | 119 ms
21,252 KB |
testcase_16 | AC | 80 ms
24,176 KB |
testcase_17 | AC | 1 ms
6,944 KB |
testcase_18 | AC | 79 ms
16,332 KB |
testcase_19 | AC | 123 ms
21,080 KB |
testcase_20 | AC | 61 ms
14,380 KB |
testcase_21 | AC | 75 ms
20,536 KB |
testcase_22 | AC | 73 ms
19,012 KB |
ソースコード
use std::io::Write; fn run() { input! { n: usize, x: [i64; n], a: [i64; n], } let mut edge = vec![]; for i in 0..n { let (p, q) = (x[i], a[i]); for d in [-q, q].iter() { let pos = p + *d; if let Ok(k) = x.binary_search(&pos) { edge.push((i, k)); } } } let graph = CSR::new(n, edge.iter().cloned()); let (len, id) = strongly_connected_components(n, |v| graph.list(v)); let mut dp = vec![0; len]; let mut ord = (0..n).collect::<Vec<_>>(); ord.sort_by_key(|v| id[*v]); for &v in ord.iter().rev() { let p = id[v]; dp[p].chmax(x[v] + a[v]); for u in graph.list(v) { dp[p] = dp[p].max(dp[id[u]]); } } let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); for i in 0..n { let ans = dp[id[i]] - x[i]; writeln!(out, "{}", ans).ok(); } } fn main() { run(); } // ---------- begin CSR ---------- pub struct CSR { size: usize, pos: Vec<u32>, list: Vec<u32>, } impl CSR { pub fn new<I>(size: usize, it: I) -> Self where I: Iterator<Item = (usize, usize)> + Clone, { let mut pos = vec![0; size + 1]; for (s, t) in it.clone() { assert!(s < size && t < size); pos[s + 1] += 1; } for i in 1..=size { pos[i] += pos[i - 1]; } let mut x = pos[..size].to_vec(); let mut list = vec![0; pos[size] as usize]; for (s, t) in it { let x = &mut x[s]; list[*x as usize] = t as u32; *x += 1; } CSR { size, pos, list } } pub fn list(&self, v: usize) -> impl Iterator<Item = usize> + '_ { assert!(v < self.size); let s = self.pos[v] as usize; let t = self.pos[v + 1] as usize; self.list[s..t].iter().map(|p| *p as usize) } } // ---------- end CSR ---------- // ---------- 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 scc ---------- pub fn strongly_connected_components<G, I>(size: usize, graph: G) -> (usize, Vec<usize>) where G: Fn(usize) -> I, I: Iterator<Item = usize>, { let mut ord = vec![0; size]; let mut res = vec![0; size]; let mut vertex = vec![]; let mut dfs = vec![]; let mut id = 0; for s in 0..size { if ord[s] > 0 { continue; } vertex.push(s); ord[s] = vertex.len(); dfs.push((s, graph(s))); while let Some((v, mut it)) = dfs.pop() { (|| { while let Some(u) = it.next() { if ord[u] == 0 { vertex.push(u); ord[u] = vertex.len(); dfs.push((v, it)); dfs.push((u, graph(u))); return; } } let low = graph(v).map(|u| ord[u]).min().unwrap_or(ord[v]); if low < ord[v] { ord[v] = low; return; } while let Some(u) = vertex.pop() { ord[u] = !0; res[u] = id; if u == v { break; } } id += 1; })(); } } res.iter_mut().for_each(|p| *p = id - 1 - *p); (id, res) } // ---------- end scc ---------- // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] 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_export] 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_export] 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 ----------