結果
問題 | No.2986 Permutation Puzzle |
ユーザー | ID 21712 |
提出日時 | 2024-12-15 10:01:43 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 25 ms / 2,000 ms |
コード長 | 4,540 bytes |
コンパイル時間 | 12,097 ms |
コンパイル使用メモリ | 407,092 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-12-15 10:02:01 |
合計ジャッジ時間 | 14,816 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,820 KB |
testcase_01 | AC | 1 ms
6,820 KB |
testcase_02 | AC | 1 ms
6,816 KB |
testcase_03 | AC | 18 ms
6,820 KB |
testcase_04 | AC | 1 ms
6,816 KB |
testcase_05 | AC | 1 ms
6,816 KB |
testcase_06 | AC | 1 ms
6,820 KB |
testcase_07 | AC | 1 ms
6,816 KB |
testcase_08 | AC | 1 ms
6,820 KB |
testcase_09 | AC | 2 ms
6,820 KB |
testcase_10 | AC | 2 ms
6,816 KB |
testcase_11 | AC | 1 ms
6,816 KB |
testcase_12 | AC | 3 ms
6,816 KB |
testcase_13 | AC | 1 ms
6,820 KB |
testcase_14 | AC | 1 ms
6,816 KB |
testcase_15 | AC | 4 ms
6,816 KB |
testcase_16 | AC | 1 ms
6,820 KB |
testcase_17 | AC | 1 ms
6,820 KB |
testcase_18 | AC | 1 ms
6,816 KB |
testcase_19 | AC | 1 ms
6,820 KB |
testcase_20 | AC | 1 ms
6,820 KB |
testcase_21 | AC | 14 ms
6,820 KB |
testcase_22 | AC | 1 ms
6,816 KB |
testcase_23 | AC | 2 ms
6,816 KB |
testcase_24 | AC | 6 ms
6,816 KB |
testcase_25 | AC | 11 ms
6,816 KB |
testcase_26 | AC | 6 ms
6,816 KB |
testcase_27 | AC | 11 ms
6,820 KB |
testcase_28 | AC | 11 ms
6,816 KB |
testcase_29 | AC | 7 ms
6,816 KB |
testcase_30 | AC | 14 ms
6,816 KB |
testcase_31 | AC | 9 ms
6,820 KB |
testcase_32 | AC | 25 ms
6,816 KB |
testcase_33 | AC | 25 ms
6,820 KB |
testcase_34 | AC | 18 ms
6,820 KB |
testcase_35 | AC | 17 ms
6,820 KB |
testcase_36 | AC | 20 ms
6,820 KB |
testcase_37 | AC | 10 ms
6,816 KB |
testcase_38 | AC | 5 ms
6,816 KB |
testcase_39 | AC | 25 ms
6,816 KB |
testcase_40 | AC | 9 ms
6,816 KB |
testcase_41 | AC | 12 ms
6,816 KB |
testcase_42 | AC | 20 ms
6,816 KB |
testcase_43 | AC | 16 ms
6,820 KB |
ソースコード
fn main() { let stdin = std::io::stdin(); let p = Problem::read(stdin.lock()); let mut ops: Vec<usize> = vec![0; p.k]; search(&p.b, p.k, &p.a, &mut ops); let mut ss: Vec<Vec<usize>> = vec![vec![]; p.k]; let mut a = p.a.clone(); for (i, &op) in ops.iter().enumerate().rev() { if op < a.n { ss[i].extend(a.select_col(op)); a.operate_col(op); } else { ss[i].extend(a.select_row(op - a.n)); a.operate_row(op - a.n); } } let mut ans: Vec<usize> = Vec::with_capacity(200); for (&op, s) in ops.iter().zip(ss.iter()) { let mut x = s[op % p.n]; let cnt = cycle(s) - 1; for _ in 0..cnt { if op < p.n { ans.push(x); } else { ans.push(x + p.n); } x = s[x]; } } use std::io::Write; let mut buf: Vec<u8> = Vec::with_capacity(2024); writeln!(&mut buf, "{}", ans.len()).unwrap(); for &op in ans.iter() { if op < p.n { writeln!(&mut buf, "C {}", op + 1).unwrap(); } else { writeln!(&mut buf, "R {}", op + 1 - p.n).unwrap(); } } let stdout = std::io::stdout(); stdout.lock().write_all(&buf).unwrap(); } fn search(g: &PPuzzle, k: usize, z: &PPuzzle, v: &mut Vec<usize>) -> bool { if k == 0 { return z.eq(g); } let k = k - 1; for &i in g.f[k].iter() { v[k] = i; let mut w = z.clone(); w.operate_col(i); if search(g, k, &w, v) { return true; } v[k] = i + g.n; let mut w = z.clone(); w.operate_row(i); if search(g, k, &w, v) { return true; } } false } fn cycle(v: &[usize]) -> usize { let mut w: Vec<usize> = vec![0; v.len()]; let mut t: Vec<usize> = v.to_vec(); let mut cnt: usize = 0; loop { for (i, &x) in v.iter().enumerate() { w[x] = t[i]; } cnt += 1; if w == v { return cnt; } t.copy_from_slice(&w); } } #[derive(Clone, Eq)] struct PPuzzle { n: usize, i_col: Vec<usize>, i_row: Vec<usize>, f: std::rc::Rc<Vec<Vec<usize>>>, } impl PPuzzle { fn at(&self, r: usize, c: usize) -> usize { self.f[self.i_row[r]][self.i_col[c]] } fn select_col(&self, c: usize) -> impl Iterator<Item = usize> + '_ { (0..self.n).map(move |r| self.at(r, c)) } fn select_row(&self, r: usize) -> impl Iterator<Item = usize> + '_ { (0..self.n).map(move |c| self.at(r, c)) } fn operate_col(&mut self, c: usize) { let s: Vec<usize> = self.select_col(c).collect(); let t: Vec<usize> = self.i_col.clone(); for (i, x) in s.into_iter().enumerate() { self.i_col[x] = t[i]; } } fn operate_row(&mut self, r: usize) { let s: Vec<usize> = self.select_row(r).collect(); let t: Vec<usize> = self.i_row.clone(); for (i, x) in s.into_iter().enumerate() { self.i_row[x] = t[i]; } } fn iter(&self) -> impl Iterator<Item = usize> + '_ { let n = self.n; (0..n).flat_map(move |r| (0..n).map(move |c| self.at(r, c))) } } impl PartialEq for PPuzzle { fn eq(&self, other: &PPuzzle) -> bool { self.iter().eq(other.iter()) } } impl std::iter::FromIterator<usize> for PPuzzle { fn from_iter<T>(iter: T) -> PPuzzle where T: IntoIterator<Item = usize>, { let x: Vec<usize> = iter.into_iter().map(|v| v - 1).collect(); let n: usize = (x.len() as f64).sqrt().floor() as usize; let f: Vec<Vec<usize>> = x.chunks(n).map(ToOwned::to_owned).collect(); PPuzzle { n, i_col: (0..n).collect(), i_row: (0..n).collect(), f: std::rc::Rc::new(f), } } } struct Problem { n: usize, k: usize, a: PPuzzle, b: PPuzzle, } impl Problem { fn read<R: std::io::Read>(reader: R) -> Problem { let mut x: Vec<usize> = std::io::read_to_string(reader) .unwrap() .split_ascii_whitespace() .map(str::parse) .filter_map(Result::ok) .collect(); let n: usize = x.drain(..1).next().unwrap(); let k: usize = x.drain(..1).next().unwrap(); let a: PPuzzle = x.drain(..n * n).collect(); let b: PPuzzle = x.into_iter().collect(); Problem { n, k, a, b } } }