結果
問題 | No.274 The Wall |
ユーザー | zeronosu77108 |
提出日時 | 2022-07-17 16:28:57 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,970 bytes |
コンパイル時間 | 12,378 ms |
コンパイル使用メモリ | 401,760 KB |
実行使用メモリ | 190,040 KB |
最終ジャッジ日時 | 2024-06-29 17:37:50 |
合計ジャッジ時間 | 13,110 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,944 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | WA | - |
testcase_10 | AC | 0 ms
6,940 KB |
testcase_11 | WA | - |
testcase_12 | AC | 11 ms
6,944 KB |
testcase_13 | AC | 1 ms
6,944 KB |
testcase_14 | AC | 4 ms
6,940 KB |
testcase_15 | AC | 8 ms
6,944 KB |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | AC | 16 ms
6,940 KB |
testcase_20 | AC | 18 ms
6,944 KB |
testcase_21 | AC | 19 ms
6,940 KB |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 20 ms
6,944 KB |
testcase_25 | AC | 20 ms
6,944 KB |
コンパイルメッセージ
warning: unused import: `proconio::marker::Usize1` --> src/main.rs:51:5 | 51 | use proconio::marker::Usize1; | ^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
ソースコード
fn main() { input! { n : usize, m : Usize1, lr : [(usize, usize); n] } let mut ts = TwoSat::new(n); for (i, &(li, ri)) in lr.iter().enumerate() { for (j, &(lj, rj)) in lr.iter().enumerate().skip(i+1) { if (li..=ri).contains(&lj) || (lj..=rj).contains(&ri) { ts.add_clause(i, false, j, false); } { let (li, ri) = (m - li, m - ri); if (li..=ri).contains(&lj) || (lj..=rj).contains(&ri) { ts.add_clause(i, true, j, false); } } { let (lj, rj) = (m - lj, m - rj); if (li..=ri).contains(&lj) || (lj..=rj).contains(&ri) { ts.add_clause(i, false, j, true); } } { let (li, ri) = (m - li, m - ri); let (lj, rj) = (m - lj, m - rj); if (li..=ri).contains(&lj) || (lj..=rj).contains(&ri) { ts.add_clause(i, true, j, true) } } } } let ans = ts.satisfiable(); println!("{}", if ans {"YES"} else {"NO"}); } // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); let mut next = || { iter.next().unwrap() }; input_inner!{next, $($r)*} }; ($($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_export] 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_export] macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ( $(read_value!($next, $t)),* ) }; ($next:expr, [ $t:tt ]) => { read_value!($next, [$t ; read_value!($next, usize)]) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, Chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, Bytes) => { read_value!($next, String).bytes().collect::<Vec<u8>>() }; ($next:expr, Usize1) => { read_value!($next, usize) - 1 }; ($next:expr, $t:ty) => { $next().parse::<$t>().expect("Parse error") }; } //https://github.com/rust-lang-ja/ac-library-rs pub mod internal_scc { pub struct Csr<E> { start: Vec<usize>, elist: Vec<E>, } impl<E> Csr<E> where E: Copy, { pub fn new(n: usize, edges: &[(usize, E)], init: E) -> Self { let mut csr = Csr { start: vec![0; n + 1], elist: vec![init; edges.len()], }; for e in edges.iter() { csr.start[e.0 + 1] += 1; } for i in 1..=n { csr.start[i] += csr.start[i - 1]; } let mut counter = csr.start.clone(); for e in edges.iter() { csr.elist[counter[e.0]] = e.1; counter[e.0] += 1; } csr } } #[derive(Copy, Clone)] struct _Edge { to: usize, } pub struct SccGraph { n: usize, edges: Vec<(usize, _Edge)>, } impl SccGraph { pub fn new(n: usize) -> Self { SccGraph { n, edges: vec![] } } pub fn num_vertices(&self) -> usize { self.n } pub fn add_edge(&mut self, from: usize, to: usize) { self.edges.push((from, _Edge { to })); } pub fn scc_ids(&self) -> (usize, Vec<usize>) { struct _Env { g: Csr<_Edge>, now_ord: usize, group_num: usize, visited: Vec<usize>, low: Vec<usize>, ord: Vec<Option<usize>>, ids: Vec<usize>, } let mut env = _Env { g: Csr::new(self.n, &self.edges, _Edge { to: 0 }), now_ord: 0, group_num: 0, visited: Vec::with_capacity(self.n), low: vec![0; self.n], ord: vec![None; self.n], ids: vec![0; self.n], }; fn dfs(v: usize, n: usize, env: &mut _Env) { env.low[v] = env.now_ord; env.ord[v] = Some(env.now_ord); env.now_ord += 1; env.visited.push(v); for i in env.g.start[v]..env.g.start[v + 1] { let to = env.g.elist[i].to; if let Some(x) = env.ord[to] { env.low[v] = std::cmp::min(env.low[v], x); } else { dfs(to, n, env); env.low[v] = std::cmp::min(env.low[v], env.low[to]); } } if env.low[v] == env.ord[v].unwrap() { loop { let u = *env.visited.last().unwrap(); env.visited.pop(); env.ord[u] = Some(n); env.ids[u] = env.group_num; if u == v { break; } } env.group_num += 1; } } for i in 0..self.n { if env.ord[i].is_none() { dfs(i, self.n, &mut env); } } for x in env.ids.iter_mut() { *x = env.group_num - 1 - *x; } (env.group_num, env.ids) }pub fn scc(&self) -> Vec<Vec<usize>> { let ids = self.scc_ids(); let group_num = ids.0; let mut counts = vec![0usize; group_num]; for &x in ids.1.iter() { counts[x] += 1; } let mut groups: Vec<Vec<usize>> = (0..ids.0).map(|_| vec![]).collect(); for i in 0..group_num { groups[i].reserve(counts[i]); } for i in 0..self.n { groups[ids.1[i]].push(i); } groups } }} pub mod twosat { use crate::internal_scc; pub struct TwoSat { n: usize, scc: internal_scc::SccGraph, answer: Vec<bool>, } impl TwoSat { pub fn new(n: usize) -> Self { TwoSat { n, answer: vec![false; n], scc: internal_scc::SccGraph::new(2 * n), } } pub fn add_clause(&mut self, i: usize, f: bool, j: usize, g: bool) { assert!(i < self.n && j < self.n); self.scc.add_edge(2 * i + !f as usize, 2 * j + g as usize); self.scc.add_edge(2 * j + !g as usize, 2 * i + f as usize); } pub fn satisfiable(&mut self) -> bool { let id = self.scc.scc_ids().1; for i in 0..self.n { if id[2 * i] == id[2 * i + 1] { return false; } self.answer[i] = id[2 * i] < id[2 * i + 1]; } true } pub fn answer(&self) -> &[bool] { &self.answer } } #[cfg(test)] mod tests { #![allow(clippy::many_single_char_names)] use super::*; #[test] fn solve_alpc_h_sample1() { let (n, d) = (3, 2); let x = [1, 2, 0i32]; let y = [4, 5, 6]; let mut t = TwoSat::new(n); for i in 0..n { for j in i + 1..n { if (x[i] - x[j]).abs() < d { t.add_clause(i, false, j, false); } if (x[i] - y[j]).abs() < d { t.add_clause(i, false, j, true); } if (y[i] - x[j]).abs() < d { t.add_clause(i, true, j, false); } if (y[i] - y[j]).abs() < d { t.add_clause(i, true, j, true); } } } assert!(t.satisfiable()); let answer = t.answer(); let mut res = vec![]; for (i, &v) in answer.iter().enumerate() { if v { res.push(x[i]) } else { res.push(y[i]); } } res.sort_unstable(); let mut min_distance = i32::max_value(); for i in 1..res.len() { min_distance = std::cmp::min(min_distance, res[i] - res[i - 1]); } assert!(min_distance >= d); } #[test] fn solve_alpc_h_sample2() { let (n, d) = (3, 3); let x = [1, 2, 0i32]; let y = [4, 5, 6]; let mut t = TwoSat::new(n); for i in 0..n { for j in i + 1..n { if (x[i] - x[j]).abs() < d { t.add_clause(i, false, j, false); } if (x[i] - y[j]).abs() < d { t.add_clause(i, false, j, true); } if (y[i] - x[j]).abs() < d { t.add_clause(i, true, j, false); } if (y[i] - y[j]).abs() < d { t.add_clause(i, true, j, true); } } } assert!(!t.satisfiable()); } }} use proconio::marker::Usize1; use twosat::*;