結果

問題 No.572 妖精の演奏
ユーザー hatoohatoo
提出日時 2017-10-12 12:53:20
言語 Rust
(1.72.1)
結果
AC  
実行時間 1,368 ms / 2,000 ms
コード長 3,169 bytes
コンパイル時間 964 ms
コンパイル使用メモリ 155,260 KB
実行使用メモリ 111,176 KB
最終ジャッジ日時 2023-08-10 23:55:29
合計ジャッジ時間 12,530 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
17,772 KB
testcase_01 AC 81 ms
17,776 KB
testcase_02 AC 81 ms
17,764 KB
testcase_03 AC 81 ms
17,760 KB
testcase_04 AC 51 ms
13,520 KB
testcase_05 AC 175 ms
28,000 KB
testcase_06 AC 246 ms
34,168 KB
testcase_07 AC 245 ms
34,148 KB
testcase_08 AC 246 ms
34,156 KB
testcase_09 AC 329 ms
41,020 KB
testcase_10 AC 1,030 ms
84,052 KB
testcase_11 AC 1,036 ms
84,036 KB
testcase_12 AC 1,039 ms
84,300 KB
testcase_13 AC 1,368 ms
111,176 KB
testcase_14 AC 1,039 ms
84,032 KB
testcase_15 AC 1,025 ms
84,036 KB
testcase_16 AC 1,021 ms
84,040 KB
testcase_17 AC 1,030 ms
83,968 KB
testcase_18 AC 144 ms
24,108 KB
testcase_19 AC 7 ms
4,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};

mod util {
    use std::io::stdin;
    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 get<T: FromStr>() -> T
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().parse().unwrap()
    }

    #[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 get2<T: FromStr, U: FromStr>() -> (T, U)
    where
        <T as FromStr>::Err: Debug,
        <U as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        let mut iter = line.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    }

    #[allow(dead_code)]
    pub fn get3<S: FromStr, T: FromStr, U: FromStr>() -> (S, T, U)
    where
        <S as FromStr>::Err: Debug,
        <T as FromStr>::Err: Debug,
        <U as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        let mut iter = line.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    }
}


#[allow(unused_macros)]
macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn main() {
    let n: usize = util::get();
    let m: usize = util::get();
    let a: Vec<Vec<usize>> = (0..m).map(|_| util::gets()).collect();

    let b = 10000;
    let mut dp = vec![vec![vec![0; m]; b + 1]; m];
    for i in 1..b + 1 {
        for k in 0..m {
            for s in 0..m {
                if i == 1 {
                    dp[s][i][k] = a[s][k];
                } else {
                    for j in 0..m {
                        dp[s][i][k] = max(dp[s][i][k], dp[s][i - 1][j] + a[j][k]);
                    }
                }
            }
        }
    }

    let c = n / b;
    let rest = n - c * b - 1;
    let mut dp2 = vec![vec![0; m]; c + 2];

    for i in 1..c + 2 {
        for k in 0..m {
            if i == c + 1 {
                for j in 0..m {
                    dp2[i][k] = max(dp2[i][k], dp2[i - 1][j] + dp[j][rest][k]);
                }
            } else {
                for j in 0..m {
                    dp2[i][k] = max(dp2[i][k], dp2[i - 1][j] + dp[j][b][k]);
                }
            }
        }
    }

    println!("{}", dp2[c + 1].iter().max().cloned().unwrap())

}
0