結果

問題 No.2509 Beam Shateki
ユーザー haihamabossuhaihamabossu
提出日時 2023-10-20 22:17:42
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1,177 ms / 2,000 ms
コード長 3,686 bytes
コンパイル時間 14,270 ms
コンパイル使用メモリ 395,304 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-20 19:36:09
合計ジャッジ時間 46,168 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod scanner {

    pub struct Scanner {
        buf: Vec<String>,
    }

    impl Scanner {
        pub fn new() -> Self {
            Self { buf: vec![] }
        }

        pub fn new_from(source: &str) -> Self {
            let source = String::from(source);
            let buf = Self::split(source);
            Self { buf }
        }

        pub fn next<T: std::str::FromStr>(&mut self) -> T {
            loop {
                if let Some(x) = self.buf.pop() {
                    return x.parse().ok().expect("");
                }
                let mut source = String::new();
                std::io::stdin().read_line(&mut source).expect("");
                self.buf = Self::split(source);
            }
        }

        fn split(source: String) -> Vec<String> {
            source
                .split_whitespace()
                .rev()
                .map(String::from)
                .collect::<Vec<_>>()
        }
    }
}

use crate::scanner::Scanner;
use std::{collections::BTreeMap, io::Write};
fn main() {
    let mut scanner = Scanner::new();
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    let t: usize = 1;
    for _ in 0..t {
        solve(&mut scanner, &mut out);
    }
}
fn solve(scanner: &mut Scanner, out: &mut std::io::BufWriter<std::io::StdoutLock>) {
    let h = scanner.next::<usize>();
    let w = scanner.next::<usize>();
    let a = (0..h)
        .map(|_| (0..w).map(|_| scanner.next::<usize>()).collect::<Vec<_>>())
        .collect::<Vec<_>>();
    let mut sum = BTreeMap::<(usize, usize, usize, usize), usize>::new();
    let mut rev = BTreeMap::<(usize, usize), Vec<(usize, usize, usize, usize)>>::new();
    for i in 0..h {
        let key = (i, 0, 0, 1);
        let mut now = 0;
        for j in 0..w {
            now += a[i][j];
            rev.entry((i, j)).or_insert(vec![]).push(key);
        }
        sum.insert(key, now);
    }
    for j in 0..w {
        let key = (0, j, 1, 0);
        let mut now = 0;
        for i in 0..h {
            now += a[i][j];
            rev.entry((i, j)).or_insert(vec![]).push(key);
        }
        sum.insert(key, now);
    }
    for i in 0..h + w - 1 {
        let key = (i, 0, !0, 1);
        let mut now = 0;
        for j in 0..w {
            if i - j < h {
                now += a[i - j][j];
                rev.entry((i - j, j)).or_insert(vec![]).push(key);
            }
        }
        sum.insert(key, now);
    }
    for i in 0..h + w - 1 {
        let key = (i, w - 1, !0, !0);
        let mut now = 0;
        for j in 0..w {
            if i - j < h {
                now += a[i - j][w - j - 1];
                rev.entry((i - j, w - j - 1)).or_insert(vec![]).push(key);
            }
        }
        sum.insert(key, now);
    }
    let mut ans = 0;
    for (k1, v1) in &sum {
        for (k2, v2) in &sum {
            if k1 == k2 {
                ans = ans.max(*v1);
                continue;
            }
            let mut res = *v1 + *v2;
            let mut ci = k1.0;
            let mut cj = k1.1;
            for _ in 0..(h + w) * 2 + 10 {
                if ci < h && cj < w {
                    let mut count = 0;
                    for k3 in &rev[&(ci, cj)] {
                        if k1 == k3 || k2 == k3 {
                            count += 1;
                        }
                    }
                    if count == 2 {
                        res -= a[ci][cj];
                        break;
                    }
                }
                ci += k1.2;
                cj += k1.3;
            }
            ans = ans.max(res);
        }
    }
    writeln!(out, "{}", ans).unwrap();
}
0