結果

問題 No.3334 I hate Image Convolution
コンテスト
ユーザー cologne
提出日時 2025-11-11 17:42:58
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 71 ms / 3,000 ms
コード長 3,052 bytes
コンパイル時間 14,179 ms
コンパイル使用メモリ 404,044 KB
実行使用メモリ 9,616 KB
最終ジャッジ日時 2025-11-11 17:43:32
合計ジャッジ時間 33,978 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

diff #

use fio::*;

fn solve(n: usize, mut a: Vec<i32>) -> Vec<Vec<i32>> {
    let mut ret = vec![vec![0; n]; n];

    a.sort();

    for r in 0..n - 1 {
        for c in 0..n - 1 {
            let idx = r * (n - 1) + c;

            let delta = a[idx] - if idx == 0 { 0 } else { a[idx - 1] };

            ret[r + 1][c + 1] += delta;
            if r + 2 < n {
                ret[r + 2][(c + 1) % 2] += delta;
                ret[r + 2][c + 1] -= delta;
            }
        }
    }

    for r in 0..n {
        for c in 0..n {
            if r >= 2 {
                ret[r][c] += ret[r - 2][c];
            }
            if c >= 2 {
                ret[r][c] += ret[r][c - 2];
            }
            if r >= 2 && c >= 2 {
                ret[r][c] -= ret[r - 2][c - 2];
            }
        }
    }

    ret
}

fn main() {
    let t = read::<usize>();

    for _ in 0..t {
        let n = read::<usize>();
        let a = read_vec::<i32>();

        assert_eq!((n - 1) * (n - 1), a.len());

        let ret = solve(n, a);

        for r in ret {
            for c in r {
                print!("{} ", c);
            }
            println!();
        }
    }
}

mod fio {
    use std::{
        cell::RefCell,
        convert::TryInto,
        fmt::Debug,
        io::{BufRead, BufWriter, StdinLock, StdoutLock, stdin, stdout},
        str::FromStr,
    };
    thread_local! {
        pub static STDIN: RefCell<StdinLock<'static>> = RefCell::new(stdin().lock());
        pub static STDOUT: RefCell<BufWriter<StdoutLock<'static>>> = RefCell::new(BufWriter::new(stdout().lock()));
    }

    #[allow(dead_code)]
    pub fn read<T: FromStr>() -> T
    where
        <T as FromStr>::Err: Debug,
    {
        read_line().parse().unwrap()
    }

    #[allow(dead_code)]
    pub fn read_vec<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        read_line()
            .split_whitespace()
            .map(|x| x.parse().unwrap())
            .collect()
    }

    #[allow(dead_code)]
    pub fn read_tuple<T, const N: usize>() -> [T; N]
    where
        T: FromStr + Debug,
        <T as FromStr>::Err: Debug,
    {
        read_vec::<T>().try_into().unwrap()
    }

    /// whitespace at the end of the line is ignored
    pub fn read_line() -> String {
        let mut s = String::new();
        STDIN.with(|cell| {
            cell.borrow_mut().read_line(&mut s).unwrap();
        });
        String::from_str(s.trim_end()).unwrap()
    }
}

#[macro_export]
macro_rules! print {
    ($($t:tt)*) => {
        fio::STDOUT.with(|cell|{
            use std::io::Write;
            write!(cell.borrow_mut(), $($t)*).unwrap()
        })};
}

#[macro_export]
macro_rules! println {
    ($($t:tt)*) => {
        fio::STDOUT.with(|cell| {
            use std::io::Write;
            writeln!(cell.borrow_mut(), $($t)*).unwrap()
        })
    };
}

#[macro_export]
macro_rules! flush {
    () => {
        fio::STDOUT.with(|cell| {
            use std::io::Write;
            cell.borrow_mut().flush().unwrap()
        });
    };
}
0