結果
問題 | No.2289 順列ソート |
ユーザー |
![]() |
提出日時 | 2023-05-05 21:44:48 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,543 bytes |
コンパイル時間 | 14,447 ms |
コンパイル使用メモリ | 378,988 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-23 06:43:11 |
合計ジャッジ時間 | 15,568 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
fn main() {let (r, w) = (std::io::stdin(), std::io::stdout());let mut sc = IO::new(r.lock(), w.lock());let n = sc.read();let mut a: Vec<usize> = sc.vec(n);let mut place = vec![0; n];for i in 0..n { place[a[i] - 1] = i; }let mut cost = 0;for i in 0..n {let idx = a[i] - 1;if i != idx {a.swap(i, place[i]);place.swap(i, idx);cost += 1;}}println!("{}", cost);}pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);impl<R: std::io::Read, W: std::io::Write> IO<R, W> {pub fn new(r: R, w: W) -> Self { Self(r, std::io::BufWriter::new(w)) }pub fn write<S: ToString>(&mut self, s: S) {use std::io::Write;self.1.write_all(s.to_string().as_bytes()).unwrap();}pub fn read<T: std::str::FromStr>(&mut self) -> T {use std::io::Read;let buf = self.0.by_ref().bytes().map(|b| b.unwrap()).skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t').take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t').collect::<Vec<_>>();unsafe { std::str::from_utf8_unchecked(&buf) }.parse().ok().expect("Parse error.")}pub fn chars(&mut self) -> Vec<char> { self.read::<String>().chars().collect() }pub fn usize0(&mut self) -> usize { self.read::<usize>() - 1 }pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {(0..n).map(|_| self.read()).collect()}}