結果

問題 No.2657 Falling Block Game
ユーザー akakimidoriakakimidori
提出日時 2024-03-01 22:19:21
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 6,266 bytes
コンパイル時間 14,694 ms
コンパイル使用メモリ 403,188 KB
実行使用メモリ 8,108 KB
最終ジャッジ日時 2024-09-29 14:21:37
合計ジャッジ時間 18,666 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 1 ms
6,820 KB
testcase_04 AC 53 ms
7,656 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 52 ms
6,816 KB
testcase_08 AC 25 ms
6,816 KB
testcase_09 AC 77 ms
7,900 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: type alias `Map` is never used
 --> src/main.rs:4:6
  |
4 | type Map<K, V> = BTreeMap<K, V>;
  |      ^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: type alias `Set` is never used
 --> src/main.rs:5:6
  |
5 | type Set<T> = BTreeSet<T>;
  |      ^^^

warning: type alias `Deque` is never used
 --> src/main.rs:6:6
  |
6 | type Deque<T> = VecDeque<T>;
  |      ^^^^^

ソースコード

diff #

use std::collections::*;
use std::io::Write;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn run() {
    input! {
        h: usize,
        w: usize,
        s: [bytes; h],
    }
    let inf = std::usize::MAX / 10;
    let mut dp = vec![0; w];
    for mut s in s.into_iter().rev() {
        let mut next = vec![inf; w];
        for _ in 0..2 {
            let rmq = RMQ::new(dp.clone());
            let mut cnt = vec![0; w + 1];
            for (i, c) in s.iter().enumerate().rev() {
                cnt[i] += cnt[i + 1];
                if *c == b'#' {
                    cnt[i] += 1;
                }
            }
            for i in 0..w {
                if s[i] == b'#' {
                    continue;
                }
                next[i] = next[i].min(dp[i]);
                let mut ok = w;
                let mut ng = i;
                while ok - ng > 1 {
                    let mid = (ok + ng) / 2;
                    if cnt[i] - cnt[mid] == 0 {
                        ok = mid;
                    } else {
                        ng = mid;
                    }
                }
                ng = i;
                while ok - ng > 1 {
                    let mid = (ok + ng) / 2;
                    if rmq.find(i, mid) <= mid - i - 1 {
                        ok = mid;
                    } else {
                        ng = mid;
                    }
                }
                next[i] = next[i].min((ok - i - 1).max(rmq.find(i, ok)));
            }
            dp.reverse();
            s.reverse();
            next.reverse();
        }
        dp = next;
        for (dp, s) in dp.iter_mut().zip(s.iter()) {
            if *s == b'#' {
                *dp = inf;
            }
        }
    }
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    for dp in dp {
        writeln!(out, "{}", dp).ok();
    }
}

fn main() {
    run();
}

// ---------- 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 ----------

pub struct RMQ<T> {
    data: Vec<T>,
    table: SparseTable<T>,
    bit: Vec<usize>,
}

impl<T> RMQ<T>
where
    T: Ord + Copy,
{
    pub fn new(data: Vec<T>) -> Self {
        assert!(!data.is_empty());
        let mut bit = vec![0; data.len()];
        let w = 8 * std::mem::size_of_val(&bit[0]);
        let mut stack: Vec<usize> = vec![];
        let mut table_ini = Vec::with_capacity((data.len() + w - 1) / w);
        for (bit, data) in bit.chunks_mut(w).zip(data.chunks(w)) {
            stack.clear();
            let mut b = 0;
            for (i, (bit, d)) in bit.iter_mut().zip(data.iter()).enumerate() {
                while stack.last().map_or(false, |x| data[*x] > *d) {
                    b ^= 1 << stack.pop().unwrap();
                }
                b |= 1 << i;
                *bit = b;
                stack.push(i);
            }
            table_ini.push(data[stack[0]]);
        }
        let table = SparseTable::new(table_ini);
        RMQ { data, table, bit }
    }
    pub fn find(&self, l: usize, r: usize) -> T {
        assert!(l < r && r <= self.data.len());
        let w = 8 * std::mem::size_of_val(&self.bit[0]);
        let r = r - 1;
        let p = l / w;
        let q = r / w;
        if p == q {
            let pos = l + (self.bit[r] >> (l % w)).trailing_zeros() as usize;
            self.data[pos]
        } else {
            let pos = l + (self.bit[l / w * w + w - 1] >> (l % w)).trailing_zeros() as usize;
            let mut res = self.data[pos];
            let pos = r / w * w + (self.bit[r] >> 0).trailing_zeros() as usize;
            res = std::cmp::min(res, self.data[pos]);
            if l / w + 1 < r / w {
                res = std::cmp::min(res, self.table.find(l / w + 1, r / w));
            }
            res
        }
    }
}

// ---------- begin sparse table (min) ----------
pub struct SparseTable<T> {
    table: Vec<Vec<T>>,
    size: usize,
}

impl<T> SparseTable<T>
where
    T: Ord + Copy,
{
    pub fn new(mut a: Vec<T>) -> Self {
        assert!(a.len() > 0);
        let size = a.len();
        let mut table = vec![];
        let mut w = 1;
        while w + 1 <= a.len() {
            let next = a
                .iter()
                .zip(a[w..].iter())
                .map(|p| std::cmp::min(*p.0, *p.1))
                .collect::<Vec<_>>();
            table.push(a);
            a = next;
            w <<= 1;
        }
        table.push(a);
        SparseTable {
            table: table,
            size: size,
        }
    }
    pub fn find(&self, l: usize, r: usize) -> T {
        assert!(l < r && r <= self.size);
        let k = (r - l + 1).next_power_of_two().trailing_zeros() as usize - 1;
        let table = &self.table[k];
        std::cmp::min(table[l], table[r - (1 << k)])
    }
}
// ---------- end sparse table (min) ----------
0