結果
| 問題 |
No.2673 A present from B
|
| コンテスト | |
| ユーザー |
ngtkana
|
| 提出日時 | 2024-03-02 00:25:48 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 2,795 bytes |
| コンパイル時間 | 11,706 ms |
| コンパイル使用メモリ | 400,632 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-09-29 23:25:02 |
| 合計ジャッジ時間 | 12,925 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 |
ソースコード
use input::input_array;
use input::input_vec;
fn main() {
let [n, _m] = input_array::<usize, 2>();
let mut a = input_vec::<usize>();
for x in &mut a {
*x -= 1;
}
let mut dist = (0..n as isize).collect::<Vec<_>>();
for &x in a.iter().rev() {
dist.swap(x, x + 1);
let forward = (0..n)
.scan(isize::MAX, |min, i| {
let min_ = *min;
*min = min_.min(dist[i] - i as isize);
Some(*min + i as isize)
})
.collect::<Vec<_>>();
let mut backward = (0..n)
.rev()
.scan(isize::MAX, |min, i| {
let min_ = *min;
*min = min_.min(dist[i] + i as isize);
Some(*min - i as isize)
})
.collect::<Vec<_>>();
backward.reverse();
dist = (0..n)
.map(|i| dist[i].min(forward[i]).min(backward[i]))
.collect();
}
println!(
"{}",
dist[1..]
.iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(" ")
);
}
// input {{{
#[allow(dead_code)]
mod input {
use std::cell::Cell;
use std::convert::TryFrom;
use std::io::stdin;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Lines;
use std::io::Stdin;
use std::str::FromStr;
use std::sync::Mutex;
use std::sync::Once;
type Server = Mutex<Lines<BufReader<Stdin>>>;
static ONCE: Once = Once::new();
pub struct Lazy(Cell<Option<Server>>);
unsafe impl Sync for Lazy {}
fn line() -> String {
static SYNCER: Lazy = Lazy(Cell::new(None));
ONCE.call_once(|| {
SYNCER
.0
.set(Some(Mutex::new(BufReader::new(stdin()).lines())));
});
unsafe {
(*SYNCER.0.as_ptr())
.as_ref()
.unwrap()
.lock()
.unwrap()
.next()
.unwrap()
.unwrap()
}
}
pub trait ForceFromStr: FromStr {
fn force_from_str(s: &str) -> Self;
}
impl<T, E> ForceFromStr for T
where
T: FromStr<Err = E>,
E: std::fmt::Debug,
{
fn force_from_str(s: &str) -> Self {
s.parse().unwrap()
}
}
pub fn input_array<T: ForceFromStr, const N: usize>() -> [T; N]
where
T: std::fmt::Debug,
{
<[_; N]>::try_from(input_vec()).unwrap()
}
pub fn input_vec<T: ForceFromStr>() -> Vec<T> {
line()
.split_whitespace()
.map(T::force_from_str)
.collect::<Vec<_>>()
}
pub fn input<T: ForceFromStr>() -> T {
T::force_from_str(&line())
}
}
// }}}
ngtkana