結果
問題 | No.2673 A present from B |
ユーザー | naut3 |
提出日時 | 2024-03-15 22:37:41 |
言語 | Rust (1.77.0 + proconio) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,242 bytes |
コンパイル時間 | 17,746 ms |
コンパイル使用メモリ | 390,324 KB |
実行使用メモリ | 13,632 KB |
最終ジャッジ日時 | 2024-09-30 02:04:27 |
合計ジャッジ時間 | 18,532 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
13,632 KB |
testcase_01 | AC | 1 ms
6,820 KB |
testcase_02 | AC | 1 ms
6,820 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 536 ms
6,816 KB |
testcase_05 | AC | 1 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![u32::MAX; N]; N]; let mut ans = vec![u32::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 u32; for j in 0..N { for k in 0..N { for l in 0..N { if dp[l][k] != u32::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![u32::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 k + 1..N { if dpn[l][k] != u32::MAX { dpn[j][k] = std::cmp::min(dpn[l][k] + diff(j, l), dpn[j][k]); } if dpn[k][l] != u32::MAX { dpn[j][l] = std::cmp::min(dpn[k][l] + diff(j, k), dpn[j][l]); } } } } 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()) } } } }