結果
問題 | No.2673 A present from B |
ユーザー | naut3 |
提出日時 | 2024-03-15 22:44:23 |
言語 | Rust (1.77.0 + proconio) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,243 bytes |
コンパイル時間 | 14,069 ms |
コンパイル使用メモリ | 377,728 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-09-30 02:12:33 |
合計ジャッジ時間 | 24,703 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 0 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 0 ms
6,816 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 256 ms
6,820 KB |
testcase_05 | AC | 1 ms
6,816 KB |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | AC | 1 ms
6,820 KB |
testcase_10 | WA | - |
testcase_11 | AC | 1 ms
6,820 KB |
testcase_12 | AC | 386 ms
6,816 KB |
testcase_13 | WA | - |
testcase_14 | AC | 706 ms
6,816 KB |
testcase_15 | AC | 139 ms
6,816 KB |
testcase_16 | AC | 126 ms
6,816 KB |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | AC | 220 ms
6,816 KB |
testcase_20 | AC | 618 ms
6,816 KB |
testcase_21 | WA | - |
testcase_22 | AC | 1 ms
6,820 KB |
testcase_23 | AC | 0 ms
6,816 KB |
testcase_24 | WA | - |
testcase_25 | AC | 1 ms
6,820 KB |
testcase_26 | AC | 1 ms
6,820 KB |
ソースコード
#![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 a..=a + 1 { 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()) } } } }