結果
| 問題 |
No.2673 A present from B
|
| コンテスト | |
| ユーザー |
naut3
|
| 提出日時 | 2024-03-15 22:25:40 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 2,981 bytes |
| コンパイル時間 | 13,551 ms |
| コンパイル使用メモリ | 390,956 KB |
| 実行使用メモリ | 813,184 KB |
| 最終ジャッジ日時 | 2024-09-30 01:50:03 |
| 合計ジャッジ時間 | 15,536 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 3 MLE * 1 -- * 20 |
ソースコード
#![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![vec![usize::MAX; N]; N]; M + 1];
for i in 0..N {
dp[0][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[0][l][k] != usize::MAX {
dp[0][j][k] = std::cmp::min(dp[0][l][k] + diff(j, l), dp[0][j][k]);
}
}
}
}
for i in 0..M {
let a = A[i];
for j in 0..N {
if j == a {
for k in 0..N {
dp[i + 1][a][k] = dp[i][a + 1][k];
}
} else if j == a + 1 {
for k in 0..N {
dp[i + 1][a + 1][k] = dp[i][a][k];
}
} else {
for k in 0..N {
dp[i + 1][j][k] = dp[i][j][k];
}
}
}
for j in 0..N {
for k in 0..N {
for l in 0..N {
if dp[i + 1][l][k] != usize::MAX {
dp[i + 1][j][k] =
std::cmp::min(dp[i + 1][l][k] + diff(j, l), dp[i + 1][j][k]);
}
}
}
}
}
let mut B = vec![];
for i in 1..N {
B.push(dp[M][0][i]);
}
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())
}
}
}
}
naut3