結果
問題 | No.1950 片道きゃっちぼーる |
ユーザー | akakimidori |
提出日時 | 2022-05-20 22:07:46 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,381 bytes |
コンパイル時間 | 12,854 ms |
コンパイル使用メモリ | 387,916 KB |
実行使用メモリ | 11,520 KB |
最終ジャッジ日時 | 2024-09-20 08:17:46 |
合計ジャッジ時間 | 15,480 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 140 ms
8,704 KB |
testcase_04 | AC | 92 ms
10,240 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 45 ms
7,424 KB |
testcase_07 | AC | 90 ms
11,520 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | AC | 59 ms
11,264 KB |
testcase_12 | AC | 57 ms
11,264 KB |
testcase_13 | AC | 50 ms
9,728 KB |
testcase_14 | AC | 46 ms
9,472 KB |
testcase_15 | WA | - |
testcase_16 | AC | 45 ms
8,064 KB |
testcase_17 | WA | - |
testcase_18 | AC | 53 ms
10,624 KB |
testcase_19 | WA | - |
testcase_20 | AC | 52 ms
11,264 KB |
testcase_21 | AC | 49 ms
9,856 KB |
testcase_22 | AC | 46 ms
9,856 KB |
ソースコード
use std::io::Write; fn run() { input! { n: usize, x: [i64; n], a: [i64; n], } let mut dsu = DSU::new(n); let mut dp = vec![0; n]; for i in 0..n { let (p, q) = (x[i], a[i]); dp[dsu.root(i)].chmax(p + q); for d in [-q, q].iter() { let pos = p + *d; if let Ok(k) = x.binary_search(&pos) { if let Some((a, b)) = dsu.unite(k, i) { let v = dp[b]; dp[a].chmax(v); } } } } let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); for i in 0..n { let ans = dp[dsu.root(i)] - x[i]; writeln!(out, "{}", ans).ok(); } } fn main() { run(); } // ---------- 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 union_find ---------- pub struct DSU { p: Vec<i32>, } impl DSU { pub fn new(n: usize) -> DSU { assert!(n < std::i32::MAX as usize); DSU { p: vec![-1; n] } } pub fn init(&mut self) { self.p.iter_mut().for_each(|p| *p = -1); } pub fn root(&self, mut x: usize) -> usize { assert!(x < self.p.len()); while self.p[x] >= 0 { x = self.p[x] as usize; } x } pub fn same(&self, x: usize, y: usize) -> bool { assert!(x < self.p.len() && y < self.p.len()); self.root(x) == self.root(y) } pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> { assert!(x < self.p.len() && y < self.p.len()); let mut x = self.root(x); let mut y = self.root(y); if x == y { return None; } if self.p[x] > self.p[y] { std::mem::swap(&mut x, &mut y); } self.p[x] += self.p[y]; self.p[y] = x as i32; Some((x, y)) } pub fn parent(&self, x: usize) -> Option<usize> { assert!(x < self.p.len()); let p = self.p[x]; if p >= 0 { Some(p as usize) } else { None } } pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize where F: FnMut(usize), { while let Some(p) = self.parent(x) { f(x); x = p; } x } pub fn size(&self, x: usize) -> usize { assert!(x < self.p.len()); let r = self.root(x); (-self.p[r]) as usize } } //---------- end union_find ---------- // ---------- 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 ----------