結果
| 問題 | No.5026 Spectrum Router |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-11-23 22:36:06 |
| 言語 | Rust (1.99.1 + proconio + num + itertools + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 1,941 ms / 2,000 ms |
| + 189µs | |
| コード長 | 10,962 bytes |
| 記録 | |
| コンパイル時間 | 1,675 ms |
| コンパイル使用メモリ | 209,184 KB |
| 実行使用メモリ | 9,904 KB |
| スコア | 1,450,263 |
| 最終ジャッジ日時 | 2026-11-23 22:42:49 |
| 合計ジャッジ時間 | 104,772 ms |
|
ジャッジサーバーID (参考情報) |
judge5_0 / judge4_0 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
コンパイルメッセージ
warning: unexpected `cfg` condition value: `local`
--> src/main.rs:306:7
|
306 | #[cfg(feature="local")]
| ^^^^^^^^^^^^^^^ help: remove the condition
|
= note: no expected values for `feature`
= help: consider adding `local` as a feature in `Cargo.toml`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default
warning: unexpected `cfg` condition value: `local`
--> src/main.rs:319:11
|
319 | #[cfg(not(feature="local"))]
| ^^^^^^^^^^^^^^^ help: remove the condition
|
= note: no expected values for `feature`
= help: consider adding `local` as a feature in `Cargo.toml`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
warning: constant `NULLP` is never used
--> src/main.rs:329:7
|
329 | const NULLP:P=P{i:!0,j:!0};
| ^^^^^
|
= note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default
warning: constant `DC` is never used
--> src/main.rs:330:7
|
330 | const DC:[char;4]=['L','U','R','D'];
| ^^
warning: constant `DX` is never used
--> src/main.rs:332:7
|
332 | const DX:[P;8]=[P{i:0,j:!0},P{i:!0,j:!0},P{i:!0,j:0},P{i:!0,j:1},P{i:0,j:1},P{i:1,j:1},P{i:1,j:0},P{i:1,j:!0}];
| ^^
warning: associated items `id`, `from`, `manh`, and `dir` are never used
--> src/main.rs:348:8
|
339 | impl P{
| ------ associated items in this implementation
...
348 | fn id(self,w:usize)->usize{
| ^^
...
352 | fn from(id:usize,w:usize)->P{
| ^^^^
...
356 | fn manh(self,p:P)->usize{
| ^^^^
...
361 | fn dir(self,p:P)->usize{
| ^^^
warning: function `iterp` is never used
--> src/main.rs:465:4
|
465 | fn iterp(h:usize,w:usize)->impl Iterator<Item=P>{
ソースコード
#![allow(non_snake_case)]
use itertools::*;
type Mat<T>=[[T;N];N];
fn main(){
let A=get_input();
let mut rot=vec![];
for i in 0..N{
rot.push(P::new(0,i));
}
for i in 0..N{
rot.push(P::new(i,N-1));
}
for i in (0..N).rev(){
rot.push(P::new(N-1,i));
}
for i in (0..N).rev(){
rot.push(P::new(i,0));
}
let (L,U,R,D)=(0,1,2,3);
let mut ans=[[0;N];N];
// [p][d] := マスpにdの向きで入ってきた
let make_ray=|ans:&Mat<usize>|->Mat<[usize;4]>{
let mut ray=[[[!0;4];N];N];
for (i,a) in A.chunks(N).enumerate(){
let dir=[D,L,U,R][i];
for (&a,&(mut p)) in izip!(a,&rot[i*64..]){
let mut dir=dir;
while p.in_range(N,N){
ray[p][dir]=a;
dir^=ans[p];
p+=DD[dir];
}
}
}
ray
};
let mut ray=make_ray(&ans);
let make_pos=|ans:&Mat<usize>,ray:&Mat<[usize;4]>|->[usize;4*N]{
let mut pos=[!0;4*N];
for i in 0..4*N{
let p=rot[i];
let dir=[U,R,D,L][i/64]^ans[p];
pos[ray[p][dir]]=i;
}
pos
};
let mut pos=make_pos(&ans,&ray);
// 最大化
let eval=|pos:&[usize]|->i64{
let mut sum=0;
for i in 0..pos.len(){
sum+=i*pos[i];
}
sum as i64
};
let mut score=eval(&pos);
let apply=|p:P,new:usize,ans:&mut Mat<usize>,ray:&mut Mat<[usize;4]>|{
ans[p]=new;
for mut dir in 0..4{
let mut p=p;
let a=ray[p][dir];
while p.in_range(N,N){
ray[p][dir]=a;
dir^=ans[p];
p+=DD[dir];
}
}
ans[p]=new;
};
let mut iter=0;
let mut valid=0;
for add in anneal_iter(4,3e2,1e0,1.95){
iter+=1;
let p='a: loop{
let p=P::new(rnd::range(0,N),rnd::range(0,N));
for i in 0..4{
for j in 0..i{
if ray[p][i]==ray[p][j]{
continue 'a;
}
}
}
break p;
};
let new=loop{
let new=*[0,1,3].choice();
if new!=ans[p]{
break new;
}
};
let xor=ans[p]^new;
// ans[p]^new で swap が発生する
let mut new_score=score;
for dir in 0..4{
let a=ray[p][dir];
let b=ray[p][dir^xor];
new_score+=a as i64*(pos[b] as i64-pos[a] as i64);
}
// 最大化: new-old>=add
if (new_score-score) as f64>=add{
valid+=1;
score=new_score;
for dir in 0..4{
let ndir=dir^xor;
if dir<ndir{
let a=ray[p][dir];
let b=ray[p][ndir];
pos.swap(a,b);
}
}
apply(p,new,&mut ans,&mut ray);
}
}
assert!(score==eval(&pos));
assert!(ray==make_ray(&ans));
assert!(pos==make_pos(&ans,&ray));
eprintln!("iter = {}",iter);
eprintln!("rate = {:.3}",valid as f64/iter as f64);
eprintln!("eval = {:.2}",score as f64/1e5);
for i in 0..N{
println!("{}",ans[i].iter().map(|&t|[0,2,!0,1][t]).join(""));
}
let mut perm=vec![!0;pos.len()];
for i in 0..pos.len(){
perm[pos[i]]=i;
}
let M=4*N;
eprintln!("score = {}",M*(M-1)/2-inversion_number(&perm));
}
static_data!{
get_input:[usize;4*N]|{
use proconio::marker::*;
proconio::input!{
_n:usize,
A:[Usize1;4*N],
}
A.try_into().unwrap()
}
}
const N:usize=64;
#[macro_export]
macro_rules! static_data{
($a:ident:$t:ty|$e:expr)=>{
fn $a()->&'static $t{
use std::sync::OnceLock;
#[allow(non_upper_case_globals)]
static $a:OnceLock<$t>=OnceLock::new();
$a.get_or_init(||$e)
}
}
}
// 最大化: new-old>=add
fn anneal_iter(skip:usize,t0:f64,t1:f64,tl:f64)->impl Iterator<Item=f64>{
assert!(0.<t1 && t1<t0);
let t1=t1/t0;
let start=get_time();
let tl=tl-start;
assert!(0.<tl);
use std::sync::OnceLock;
static LOGS:OnceLock<[f64;65536]>=OnceLock::new();
let log=LOGS.get_or_init(||{
let mut log=[0.;65536];
for i in 0..65536{
log[i]=((i as f64+0.5)/65536.).ln();
}
rnd::shuffle(&mut log);
log
});
let mut iter=0;
let mut heat=0.;
assert!(skip<usize::BITS as usize);
let mask=(1<<skip)-1;
std::iter::from_fn(move||{
if iter&mask==0{
let time=(get_time()-start)/tl;
if time>=1.{
return None;
}
heat=t0*t1.powf(time);
}
iter+=1;
Some(heat*log[iter&65535])
})
}
#[allow(unused)]
mod rnd{
static mut X2:u32=12345;
static mut X3:u32=0xcafef00d;
static mut C_X1:u64=0xd15ea5e5<<32|23456;
pub fn set_seed(seed:u64){
unsafe{
C_X1^=seed as u64;
}
}
pub fn next()->u32{
unsafe{
let x=X3 as u64*3487286589;
let ret=(X3^X2)+(C_X1 as u32^(x>>32) as u32);
X3=X2;
X2=C_X1 as u32;
C_X1=x+(C_X1>>32);
ret
}
}
pub fn next64()->u64{
(next() as u64)<<32|next() as u64
}
pub fn nextf()->f64{
f64::from_bits(0x3ff0000000000000|(next() as u64)<<20)-1.
}
pub fn get(n:usize)->usize{
assert!(0<n && n<=u32::MAX as usize);
next() as usize*n>>32
}
pub fn range(a:usize,b:usize)->usize{
assert!(a<b);
get(b-a)+a
}
pub fn range_skip(a:usize,b:usize,skip:usize)->usize{
assert!(a<=skip && skip<b);
let n=range(a,b-1);
n+(skip<=n) as usize
}
pub fn rangei(a:i64,b:i64)->i64{
assert!(a<b);
get((b-a) as usize) as i64+a
}
pub fn shuffle<T>(a:&mut [T]){
for i in (1..a.len()).rev(){
a.swap(i,get(i+1));
}
}
pub fn shuffle_iter<T:Copy>(a:&mut [T])->impl Iterator<Item=T>{
(0..a.len()).rev().map(|i|{
a.swap(i,get(i+1));
a[i]
})
}
}
#[allow(unused)]
trait RandomChoice{
type Output;
fn choice(&self)->&Self::Output;
}
impl<T> RandomChoice for [T]{
type Output=T;
fn choice(&self)->&T{
&self[rnd::get(self.len())]
}
}
#[cfg(feature="local")]
#[allow(unused)]
fn get_time()->f64{
static START:std::sync::OnceLock<f64>=std::sync::OnceLock::new();
let now=||{
let mut ts=libc::timespec{tv_sec:0,tv_nsec:0};
unsafe{libc::clock_gettime(libc::CLOCK_PROCESS_CPUTIME_ID,&mut ts);}
ts.tv_sec as f64+ts.tv_nsec as f64*1e-9
};
let start=*START.get_or_init(now);
(now()-start)*1.
}
#[cfg(not(feature="local"))]
fn get_time()->f64{
static START:std::sync::OnceLock<std::time::Instant>=std::sync::OnceLock::new();
let now=||std::time::Instant::now();
let start=*START.get_or_init(now);
now().duration_since(start).as_secs_f64()
}
const NULLP:P=P{i:!0,j:!0};
const DC:[char;4]=['L','U','R','D'];
const DD:[P;4]=[P{i:0,j:!0},P{i:!0,j:0},P{i:0,j:1},P{i:1,j:0}];
const DX:[P;8]=[P{i:0,j:!0},P{i:!0,j:!0},P{i:!0,j:0},P{i:!0,j:1},P{i:0,j:1},P{i:1,j:1},P{i:1,j:0},P{i:1,j:!0}];
#[derive(Clone,Copy,PartialEq,Eq,PartialOrd,Ord,Hash,Default)]
struct P{
i:usize,
j:usize,
}
impl P{
fn new(i:usize,j:usize)->P{
P{i,j}
}
fn in_range(self,h:usize,w:usize)->bool{
self.i<h && self.j<w
}
fn id(self,w:usize)->usize{
self.i*w+self.j
}
fn from(id:usize,w:usize)->P{
P::new(id/w,id%w)
}
fn manh(self,p:P)->usize{
let abs_diff=|a,b|(a as i64-b as i64).abs() as usize;
abs_diff(self.i,p.i)+abs_diff(self.j,p.j)
}
fn dir(self,p:P)->usize{
if self.i==p.i{
if self.j-1==p.j{
0
} else{
assert!(self.j+1==p.j);
2
}
} else{
if self.i-1==p.i{
1
} else{
assert!(self.i+1==p.i);
3
}
}
}
}
impl std::fmt::Debug for P{
fn fmt(&self,f:&mut std::fmt::Formatter)->std::fmt::Result{
write!(f,"({}, {})",self.i,self.j)
}
}
impl std::ops::Add for P{
type Output=P;
fn add(self,a:P)->P{
P{
i:self.i+a.i,
j:self.j+a.j,
}
}
}
impl std::ops::Sub for P{
type Output=P;
fn sub(self,a:P)->P{
P{
i:self.i-a.i,
j:self.j-a.j,
}
}
}
impl std::ops::Mul<usize> for P{
type Output=P;
fn mul(self,a:usize)->P{
P{
i:self.i*a,
j:self.j*a,
}
}
}
impl std::ops::Div<usize> for P{
type Output=P;
fn div(self,a:usize)->P{
P{
i:self.i/a,
j:self.j/a,
}
}
}
impl std::ops::Neg for P{
type Output=P;
fn neg(self)->P{
P{
i:self.i.wrapping_neg(),
j:self.j.wrapping_neg(),
}
}
}
macro_rules! impl_p_ops{
($t:ty,$assign_trait:ident,$assign_func:ident,$op:tt)=>{
impl std::ops::$assign_trait<$t> for P{
fn $assign_func(&mut self,a:$t){
*self=*self $ op a;
}
}
}
}
impl_p_ops!(P,AddAssign,add_assign,+);
impl_p_ops!(P,SubAssign,sub_assign,-);
impl_p_ops!(usize,MulAssign,mul_assign,*);
impl_p_ops!(usize,DivAssign,div_assign,/);
macro_rules! impl_p_index{
($t:ty)=>{
impl<T:std::ops::Index<usize>> std::ops::Index<P> for $t{
type Output=T::Output;
fn index(&self,idx:P)->&T::Output{
&self[idx.i][idx.j]
}
}
impl<T:std::ops::IndexMut<usize>> std::ops::IndexMut<P> for $t{
fn index_mut(&mut self,idx:P)->&mut T::Output{
&mut self[idx.i][idx.j]
}
}
}
}
impl_p_index!([T]);
impl_p_index!(Vec<T>);
fn iterp(h:usize,w:usize)->impl Iterator<Item=P>{
(0..h).map(move|i|(0..w).map(move|j|P::new(i,j))).flatten()
}
// i<j で a[i]>a[j] となる(i,j)の数
fn inversion_number<T:Ord>(a:&[T])->usize{
let mut is=(0..a.len()).collect::<Vec<_>>();
is.sort_by_key(|&i|&a[i]); // stable sort
let mut fen=ac_library::FenwickTree::<usize>::new(is.len(),0);
let mut cnt=0;
for &i in is.iter().rev(){
fen.add(i,1);
cnt+=fen.sum(..i);
}
cnt
}