結果

問題 No.5026 Spectrum Router
コンテスト
ユーザー t33f
提出日時 2026-11-23 21:52:35
言語 Rust
(1.99.1 + proconio + num + itertools + ACL)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
AC  
実行時間 1,913 ms / 2,000 ms
+ 213µs
コード長 7,045 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,346 ms
コンパイル使用メモリ 197,780 KB
実行使用メモリ 6,400 KB
スコア 1,433,987
最終ジャッジ日時 2026-11-23 21:54:42
合計ジャッジ時間 104,922 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge1_0
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `*`
 --> src/main.rs:2:28
  |
2 | use itertools::{Itertools, *};
  |                            ^
  |
  = note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default

ソースコード

diff #
raw source code

#![allow(non_snake_case)]
use itertools::{Itertools, *};
use proconio::{input, marker::Usize1, source::once::OnceSource};

const N: usize = 64;

#[derive(Clone)]
struct Input {
    a: Vec<usize>,
}

fn read_input<R: std::io::BufRead>(reader: R) -> Input {
    let source = OnceSource::new(reader);
    input! {
        from source,
        _n: usize,
        a: [Usize1; 4 * N],
    }
    Input { a }
}

fn read_input_from_stdin() -> Input {
    read_input(std::io::BufReader::new(std::io::stdin()))
}

fn read_input_from_file(f: &std::fs::File) -> Input {
    read_input(std::io::BufReader::new(f))
}

type Output = Vec<Vec<u8>>;

// unsigned int xor128() {
//     static unsigned int x=123456789, y=362436069, z=521288629, w=88675123;
//     unsigned int t;
//     t=(x^(x<<11)); x=y; y=z; z=w;
//     return (w=(w^(w>>19))^(t^(t>>8)));
// }

// inline bool rand_bool(double prob) {
//     constexpr double x = 1LL<<32; // uint_max+1
//     return xor128() < prob * x;
// }

struct Xor128 {
    x: u32,
    y: u32,
    z: u32,
    w: u32,
}

impl Xor128 {
    fn new(seed: u32) -> Self {
        let mut rng = Xor128 {
            x: 123456789,
            y: 362436069,
            z: 521288629,
            w: seed,
        };
        for _ in 0..100 {
            rng.next();
        }
        rng
    }

    fn next(&mut self) -> u32 {
        let t = self.x ^ (self.x << 11);
        self.x = self.y;
        self.y = self.z;
        self.z = self.w;
        self.w = self.w ^ (self.w >> 19) ^ (t ^ (t >> 8));
        self.w
    }

    fn random_bool(&mut self, prob: f64) -> bool {
        let x = 1u64 << 32; // uint_max+1
        (self.next() as u64) < (prob * x as f64) as u64
    }

    fn random_range(&mut self, range: std::ops::Range<usize>) -> usize {
        let len = range.end - range.start;
        range.start + (self.next() as usize % len)
    }
}

mod solver1 {
    use crate::*;

    // returns inversion of permutation
    fn inversion_perm(p: &[usize]) -> u64 {
        let incr = |i: usize, bit: &mut Vec<u64>| {
            let mut x = i + 1;
            while x < bit.len() {
                bit[x] += 1;
                x += 1 << x.trailing_zeros();
            }
        };
        let get = |i: usize, bit: &Vec<u64>| -> u64 {
            let mut x = i + 1;
            let mut ans = 0;
            while x > 0 {
                ans += bit[x];
                x -= 1 << x.trailing_zeros();
            }
            ans
        };

        let n = p.len();
        let mut bit = vec![0; n + 1];
        let mut ans = 0;
        for i in (0..n).rev() {
            ans += get(p[i], &bit);
            incr(p[i], &mut bit);
        }

        ans
    }

    fn calculate_score(ans: &Output, a: &Vec<usize>) -> i64 {
        let mut b = [0; 4 * N];
        for i in 0..4 * N as i32 {
            let (mut x, mut y, mut dx, mut dy) = if i < N as i32 {
                (0, i, 1, 0)
            } else if i < 2 * N as i32 {
                (i - N as i32, N as i32 - 1, 0, -1)
            } else if i < 3 * N as i32 {
                (N as i32 - 1, 3 * N as i32 - i - 1, -1, 0)
            } else {
                (4 * N as i32 - i - 1, 0, 0, 1)
            };

            while 0 <= x && x < N as i32 && 0 <= y && y < N as i32 {
                match ans[x as usize][y as usize] {
                    0 => {
                        x += dx;
                        y += dy;
                    }
                    1 => {
                        std::mem::swap(&mut dx, &mut dy);
                        dx *= -1;
                        dy *= -1;
                        x += dx;
                        y += dy;
                    }
                    2 => {
                        std::mem::swap(&mut dx, &mut dy);
                        x += dx;
                        y += dy;
                    }
                    _ => unreachable!(),
                }
            }

            if x < 0 {
                b[y as usize] = a[i as usize];
            } else if y >= N as i32 {
                b[x as usize + N] = a[i as usize];
            } else if x >= N as i32 {
                b[3 * N - y as usize - 1] = a[i as usize];
            } else {
                b[4 * N - x as usize - 1] = a[i as usize];
            }
        }

        (4 * N * (4 * N - 1) / 2) as i64 - inversion_perm(&b) as i64
    }

    pub fn solve(input: &Input, timer: Timer) -> Output {
        let a = &input.a;
        let mut cur_ans = vec![vec![0; N]; N];
        let mut cur_score = calculate_score(&cur_ans, a);
        let mut best_ans = vec![vec![0; N]; N];
        let mut best_score = cur_score;
        let mut rng = Xor128::new(1);
        let accept = |diff: i64, rng: &mut Xor128| -> bool {
            if diff >= 0 {
                true
            } else {
                let progress =
                    timer.get_elapsed_time() as f64 / TIME_LIMIT as f64;
                let start_temp = 1.0;
                let end_temp = 0.01;
                let temp = start_temp + progress * (end_temp - start_temp);
                let prob = (diff as f64 / temp).exp();
                rng.random_bool(prob)
            }
        };
        for iter in 0.. {
            if timer.get_elapsed_time() > TIME_LIMIT {
                eprintln!("{iter} {best_score}");
                break;
            }

            let i = rng.random_range(0..N);
            let j = rng.random_range(0..N);
            let x = loop {
                let x = rng.random_range(0..3) as u8;
                if x != cur_ans[i][j] {
                    break x;
                }
            };
            let orig = cur_ans[i][j];
            cur_ans[i][j] = x;
            let new_score = calculate_score(&cur_ans, a);
            let diff = new_score - cur_score;
            if accept(diff, &mut rng) {
                cur_score = new_score;
                if cur_score > best_score {
                    best_score = cur_score;
                    best_ans = cur_ans.clone();
                    // eprintln!("{iter} {best_score}");
                }
            } else {
                cur_ans[i][j] = orig;
            }
        }

        best_ans
    }
}

fn solve(input: &Input, timer: Timer) -> Output {
    solver1::solve(input, timer)
}

fn write_output(ans: &Output) {
    for row in ans {
        println!("{}", row.iter().join(""));
    }
}

fn main() {
    let timer = Timer::new();
    let input = {
        std::env::args()
            .nth(1)
            .map(|file| std::fs::File::open(file).ok())
            .flatten()
            .map(|f| read_input_from_file(&f))
            .unwrap_or_else(|| read_input_from_stdin())
    };

    let ans = solve(&input, timer);
    write_output(&ans);
}

#[derive(Clone, Copy)]
struct Timer {
    start: std::time::Instant,
}

impl Timer {
    fn new() -> Timer {
        let start = std::time::Instant::now();
        Timer { start }
    }

    fn get_elapsed_time(&self) -> u64 {
        self.start.elapsed().as_millis() as u64
    }
}

const TIME_LIMIT: u64 = 1950;
0