結果
問題 | No.2673 A present from B |
ユーザー | naut3 |
提出日時 | 2024-03-15 22:28:57 |
言語 | Rust (1.77.0 + proconio) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,094 bytes |
コンパイル時間 | 13,097 ms |
コンパイル使用メモリ | 405,408 KB |
実行使用メモリ | 13,636 KB |
最終ジャッジ日時 | 2024-09-30 01:54:05 |
合計ジャッジ時間 | 17,630 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
13,636 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,820 KB |
testcase_03 | AC | 0 ms
6,820 KB |
testcase_04 | AC | 821 ms
6,820 KB |
testcase_05 | AC | 0 ms
6,816 KB |
testcase_06 | TLE | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
ソースコード
#![allow(non_snake_case, unused_imports, unused_must_use)] use std::io::{self, prelude::*}; use std::str; fn main() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); macro_rules! input { ($T: ty) => { scan.token::<$T>() }; ($T: ty, $N: expr) => { (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>() }; } let N = input!(usize); let M = input!(usize); let A = input!(usize, M); let A = (0..M).map(|i| A[i] - 1).collect::<Vec<_>>(); let mut dp = vec![vec![usize::MAX; N]; N]; let mut ans = vec![usize::MAX; N]; for i in 0..N { dp[i][i] = 0; } let diff = |a: usize, b: usize| (a as isize - b as isize).abs() as usize; for j in 0..N { for k in 0..N { for l in 0..N { if dp[l][k] != usize::MAX { dp[j][k] = std::cmp::min(dp[l][k] + diff(j, l), dp[j][k]); } } } } for i in 1..N { ans[i] = std::cmp::min(ans[i], dp[0][i]); } for i in 0..M { let mut dpn = vec![vec![usize::MAX; N]; N]; let a = A[i]; for j in 0..N { if j == a { for k in 0..N { dpn[a][k] = dp[a + 1][k]; } } else if j == a + 1 { for k in 0..N { dpn[a + 1][k] = dp[a][k]; } } else { for k in 0..N { dpn[j][k] = dp[j][k]; } } } for j in 0..N { for k in 0..N { for l in 0..N { if dpn[l][k] != usize::MAX { dpn[j][k] = std::cmp::min(dpn[l][k] + diff(j, l), dpn[j][k]); } } } } dp = dpn; for i in 1..N { ans[i] = std::cmp::min(ans[i], dp[0][i]); } } let B = &ans[1..]; writeln!( out, "{}", B.iter() .map(|x| x.to_string()) .collect::<Vec<_>>() .join(" ") ); } struct Scanner<R> { reader: R, buf_str: Vec<u8>, buf_iter: str::SplitWhitespace<'static>, } impl<R: BufRead> Scanner<R> { fn new(reader: R) -> Self { Self { reader, buf_str: vec![], buf_iter: "".split_whitespace(), } } fn token<T: str::FromStr>(&mut self) -> T { loop { if let Some(token) = self.buf_iter.next() { return token.parse().ok().expect("Failed parse"); } self.buf_str.clear(); self.reader .read_until(b'\n', &mut self.buf_str) .expect("Failed read"); self.buf_iter = unsafe { let slice = str::from_utf8_unchecked(&self.buf_str); std::mem::transmute(slice.split_whitespace()) } } } }