結果

問題 No.2696 Sign Creation
ユーザー nautnaut
提出日時 2024-03-22 23:04:11
言語 Rust
(1.77.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 6,225 bytes
コンパイル時間 11,512 ms
コンパイル使用メモリ 392,880 KB
実行使用メモリ 57,984 KB
最終ジャッジ日時 2024-05-17 18:18:32
合計ジャッジ時間 13,500 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 0 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 98 ms
11,392 KB
testcase_14 AC 32 ms
6,944 KB
testcase_15 AC 31 ms
6,940 KB
testcase_16 AC 92 ms
12,032 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 12 ms
6,940 KB
testcase_19 AC 35 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 25 ms
7,168 KB
testcase_22 AC 18 ms
6,940 KB
testcase_23 AC 28 ms
6,940 KB
testcase_24 AC 29 ms
6,940 KB
testcase_25 AC 45 ms
6,944 KB
testcase_26 AC 5 ms
6,940 KB
testcase_27 AC 25 ms
6,940 KB
testcase_28 AC 22 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 219 ms
57,984 KB
testcase_32 AC 1 ms
6,940 KB
testcase_33 AC 1 ms
6,940 KB
testcase_34 AC 0 ms
6,944 KB
testcase_35 AC 0 ms
6,940 KB
testcase_36 AC 1 ms
6,940 KB
testcase_37 AC 1 ms
6,944 KB
testcase_38 AC 1 ms
6,944 KB
testcase_39 WA -
testcase_40 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::io::{self, prelude::*};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let H = input!(isize);
    let W = input!(isize);
    let N = input!(usize);
    let D = input!(isize);

    let dist = |y1: isize, x1: isize, y2: isize, x2: isize| (y1 - y2).abs() + (x1 - x2).abs();

    let mut points = vec![];
    let mut has_star = vec![vec![usize::MAX; W as usize + 1]; H as usize + 1];

    for i in 0..N {
        let (y, x) = (input!(isize), input!(isize));
        points.push((y, x));
        has_star[y as usize][x as usize] = i;
    }

    let mut dydx = vec![];

    for dy in -D..=D {
        for dx in -D..=D {
            if dist(dy, dx, 0, 0) <= D {
                dydx.push((dy, dx));
            }
        }
    }

    let mut graph = vec![vec![]; N];

    for i in 0..N {
        let (y, x) = points[i];

        for &(dy, dx) in dydx.iter() {
            if dist(dy, dx, 0, 0) <= D {
                if y + dy >= 1 && y + dy <= H && x + dx >= 1 && x + dx <= W {
                    if has_star[(y + dy) as usize][(x + dx) as usize] != usize::MAX {
                        graph[i].push(has_star[(y + dy) as usize][(x + dx) as usize]);
                    }
                }
            }
        }
    }

    let mut root = (0..N).collect::<Vec<_>>();
    let mut seen = vec![false; N];
    let mut q = std::collections::VecDeque::default();

    for i in 0..N {
        if seen[i] {
            continue;
        }

        q.push_front(i);
        seen[i] = true;

        while let Some(now) = q.pop_front() {
            for &nxt in graph[now].iter() {
                if seen[nxt] {
                    continue;
                }

                seen[nxt] = true;
                root[nxt] = i;
                q.push_back(nxt);
            }
        }
    }

    let mut original_ans = 0;

    let mut cnt = vec![0; N];

    for &r in root.iter() {
        cnt[r] += 1;
    }

    for i in 0..N {
        if cnt[i] >= 2 {
            original_ans += 1;
        }
    }

    let mut ans_max = usize::MIN;
    let mut ans_min = usize::MAX;

    let mut seen_grid = vec![vec![false; W as usize + 1]; H as usize + 1];

    for i in 0..N {
        let (y, x) = points[i];

        for &(dy, dx) in dydx.iter() {
            if dist(dy, dx, 0, 0) <= D {
                if y + dy >= 1 && y + dy <= H && x + dx >= 1 && x + dx <= W {
                    // put star (y + dy, x + dx)
                    if seen_grid[(y + dy) as usize][(x + dx) as usize] {
                        continue;
                    }

                    seen_grid[(y + dy) as usize][(x + dx) as usize] = true;

                    let mut roots = std::collections::BTreeSet::new();

                    for &(dx2, dy2) in dydx.iter() {
                        if dist(dy2, dx2, 0, 0) <= D {
                            if y + dy + dy2 >= 1
                                && y + dy + dy2 <= H
                                && x + dx + dx2 >= 1
                                && x + dx + dx2 <= W
                            {
                                if has_star[(y + dy + dy2) as usize][(x + dx + dx2) as usize]
                                    != usize::MAX
                                {
                                    roots.insert(
                                        root[has_star[(y + dy + dy2) as usize]
                                            [(x + dx + dx2) as usize]],
                                    );
                                }
                            }
                        }
                    }

                    let ans_now;
                    let mut root_cnt = 0;
                    let mut alone_cnt = 0;

                    for &r in roots.iter() {
                        if cnt[r] >= 2 {
                            root_cnt += 1;
                        } else if cnt[r] == 1 {
                            alone_cnt += 1;
                        }
                    }

                    if alone_cnt >= 1 && roots.len() == alone_cnt {
                        // 孤立した星とだけ接続した場合
                        ans_now = original_ans + 1;
                    } else if root_cnt == 0 && alone_cnt == 0 {
                        // 何も接続しない場合
                        ans_now = original_ans;
                    } else {
                        // すでに星座をなす星と接続した場合
                        ans_now = original_ans + 1 - root_cnt;
                    }

                    ans_max = std::cmp::max(ans_max, ans_now);
                    ans_min = std::cmp::min(ans_min, ans_now);
                }
            }
        }
    }

    for y in 1..=H as usize {
        for x in 1..=W as usize {
            if !seen_grid[y][x] {
                ans_max = std::cmp::max(ans_max, original_ans);
                ans_min = std::cmp::min(ans_min, original_ans);
            }
        }
    }

    writeln!(out, "{} {}", ans_min, ans_max);
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}
0