結果

問題 No.655 E869120 and Good Triangles
ユーザー hatoohatoo
提出日時 2018-02-24 19:15:41
言語 Rust
(1.77.0 + proconio)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 7,773 bytes
コンパイル時間 12,064 ms
コンパイル使用メモリ 386,952 KB
最終ジャッジ日時 2024-11-14 20:22:39
合計ジャッジ時間 12,723 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
error[E0308]: mismatched types
  --> src/main.rs:64:22
   |
64 |         .filter_map(|&(dx, dy)| {
   |                      ^^^^^^^^^
   |                      |
   |                      expected `({integer}, {integer})`, found `&_`
   |                      expected due to this
   |
   = note:  expected tuple `({integer}, {integer})`
           found reference `&_`

error[E0308]: mismatched types
  --> src/main.rs:95:26
   |
95 |             .filter_map(|&(dx, dy)| {
   |                          ^^^^^^^^^
   |                          |
   |                          expected `({integer}, {integer})`, found `&_`
   |                          expected due to this
   |
   = note:  expected tuple `({integer}, {integer})`
           found reference `&_`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `main` (bin "main") due to 2 previous errors

ソースコード

diff #

/**
 *  _           _                 __                            _   _ _   _                                 _                    _                  _
 * | |         | |               / /                           | | (_) | (_)                               | |                  (_)                | |
 * | |__   __ _| |_ ___   ___   / /__ ___  _ __ ___  _ __   ___| |_ _| |_ ___   _____ ______ _ __ _   _ ___| |_ ______ ___ _ __  _ _ __  _ __   ___| |_ ___
 * | '_ \ / _` | __/ _ \ / _ \ / / __/ _ \| '_ ` _ \| '_ \ / _ \ __| | __| \ \ / / _ \______| '__| | | / __| __|______/ __| '_ \| | '_ \| '_ \ / _ \ __/ __|
 * | | | | (_| | || (_) | (_) / / (_| (_) | | | | | | |_) |  __/ |_| | |_| |\ V /  __/      | |  | |_| \__ \ |_       \__ \ | | | | |_) | |_) |  __/ |_\__ \
 * |_| |_|\__,_|\__\___/ \___/_/ \___\___/|_| |_| |_| .__/ \___|\__|_|\__|_| \_/ \___|      |_|   \__,_|___/\__|      |___/_| |_|_| .__/| .__/ \___|\__|___/
 *                                                  | |                                                                           | |   | |
 *                                                  |_|                                                                           |_|   |_|
 *
 * https://github.com/hatoo/competitive-rust-snippets
 */
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
mod util {
    use std::io::{stdin, stdout, BufWriter, StdoutLock};
    use std::str::FromStr;
    use std::fmt::Debug;
    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }
    #[allow(dead_code)]
    pub fn chars() -> Vec<char> {
        line().chars().collect()
    }
    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
    #[allow(dead_code)]
    pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) {
        let out = stdout();
        let writer = BufWriter::new(out.lock());
        f(writer)
    }
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; ( $ t : ty ;; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ;; ) ) . collect ::< Vec < _ >> ( ) } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { println ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }

const INF: u64 = 1 << 30;

#[allow(dead_code)]
fn adjacent6(x: usize, y: usize, s: usize) -> Vec<(usize, usize)> {
    [(-1, -1), (-1, 0), (0, -1), (0, 1), (1, 0), (1, 1)]
        .into_iter()
        .filter_map(|&(dx, dy)| {
            let nx = x as isize + dx;
            let ny = y as isize + dy;

            if nx >= 0 && ny >= 0 && ny <= nx && nx < s as isize {
                Some((nx as usize, ny as usize))
            } else {
                None
            }
        })
        .collect()
}

#[allow(dead_code)]
fn main() {
    let (n, k, p) = get!(usize, usize, u64);

    let xy = get!(usize, usize; k);

    let mut que: VecDeque<(usize, usize)> = xy.into_iter().map(|(x, y)| (x - 1, y - 1)).collect();
    let mut triangle = vec![vec![INF; n]; n];

    for &(y, x) in &que {
        triangle[y][x] = 0;
    }

    // let mut cnt = 0;
    while let Some((y, x)) = que.pop_front() {
        // cnt += 1;
        for (i, j) in [(-1, -1), (-1, 0), (0, -1), (0, 1), (1, 0), (1, 1)]
            .into_iter()
            .filter_map(|&(dx, dy)| {
                let nx = y as isize + dx;
                let ny = x as isize + dy;

                if nx >= 0 && ny >= 0 && ny <= nx && nx < n as isize {
                    Some((nx as usize, ny as usize))
                } else {
                    None
                }
            }) {
            if triangle[i][j] > triangle[y][x] + 1 {
                triangle[i][j] = triangle[y][x] + 1;
                que.push_back((i, j));
            }
        }
    }

    let mut imos = vec![vec![0; n + 2]; n + 2];
    for i in 0..n {
        for j in 0..i + 1 {
            imos[i + 1][j + 1] = triangle[i][j];
        }
    }

    for i in 0..n + 1 {
        for j in 1..n + 1 {
            imos[i][j] += imos[i][j - 1];
        }
    }

    for i in (0..n).rev() {
        for j in 0..n + 1 {
            imos[i][j] += imos[i + 1][j];
        }
    }

    let mut dp = vec![vec![0; n + 1]; n + 1];
    for i in (0..n).rev() {
        for j in 0..i + 1 {
            let mut x = triangle[i][j];
            if i + 1 < n {
                x += dp[i + 1][j];
                x += dp[i + 1][j + 1];
            }
            if i + 2 < n {
                x -= dp[i + 2][j + 1];
            }
            dp[i][j] = x;
        }
    }

    /*
    let calc = |i: usize, j: usize, h: usize| -> usize {
        let mut s = dp[i][j];
        if i + h >= n {
            return s;
        }

        let sqr = imos[n][j + h] + imos[i + h][j] - imos[n][j] - imos[i + h][j + h];

        // debug!(s, sqr, dp[i + h][j + h]);

        s -= sqr;
        s -= dp[i + h][j + h];
        s
    };
    */
    #[inline(always)]
    fn calc(
        i: usize,
        j: usize,
        // n: usize,
        h: usize,
        dp: &[Vec<u64>],
        imos: &[Vec<u64>],
    ) -> u64 {
        /*
        if i + h >= n {
            return dp[i][j];
        }
        */
        //debug!(i, j, h);
        //dp[i][j] - (imos[n][j + h] + imos[i + h][j] - imos[n][j] - imos[i + h][j + h])
        //- dp[i + h][j + h]
        //
        // dp[i][j] + imos[i + h + 1][j] - imos[i + h + 1][j + h] - dp[i + h][j + h]
        unsafe {
            dp.get_unchecked(i).get_unchecked(j) + imos.get_unchecked(i + h + 1).get_unchecked(j)
                - imos.get_unchecked(i + h + 1).get_unchecked(j + h)
                - dp.get_unchecked(i + h).get_unchecked(j + h)
        }
    }

    /*
    debug!(triangle);
    debug!(dp);
    debug!(imos);

    debug!(calc(0, 0, 3, &dp, &imos));
    */

    let mut hs = vec![vec![0u16; n]; n];

    let mut ans = 0;

    for i in (0..n).rev() {
        for j in 0..i + 1 {
            let mut r = if i + 1 < n {
                min(hs[i + 1][j] as usize, hs[i + 1][j + 1] as usize) + 1
            } else {
                n - i
            };
            let mut t = r;
            while r > 0 && calc(i, j, r, &dp, &imos) >= p {
                t = r;
                r -= 1;
            }
            hs[i][j] = t as u16;
            ans += n - i - r;
        }
    }
    println!("{}", ans);
}
0