macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, Chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, Usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } #[allow(unused)] #[inline] fn get_time()->f64{ static mut START:f64=-1.; let t=std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs_f64(); unsafe{ if START<0.{START=t;} t-START } } #[cfg(local)] #[allow(unused)] macro_rules! debug{ ()=>{ eprintln!(); }; ($x:literal)=>{ eprintln!("{:?}",$x); }; ($x:expr)=>{ eprintln!("{}: {:?}",stringify!($x),$x); }; ($x:literal,$($l:expr),*)=>{ eprint!("{:?}, ",$x); debug!($($l),*); }; ($x:expr,$($l:expr),*)=>{ eprint!("{}: {:?}, ",stringify!($x),$x); debug!($($l),*); } } #[cfg(not(local))] #[allow(unused)] macro_rules! debug{ ($($_:expr),*)=>{} } #[allow(unused)] mod rnd { static mut S:usize=88172645463325252; #[inline] pub fn next()->usize{ unsafe{ S^=S<<7; S^=S>>9; S } } #[inline] pub fn nextf()->f64{ unsafe{ std::mem::transmute::<_,f64>(0x3ff0000000000000|next()&0xfffffffffffff)-1. } } // [a,b) #[inline] pub fn range(a:usize,b:usize)->usize{ next()%(b-a)+a } #[inline] pub fn rangei(a:i64,b:i64)->i64{ (next()>>1) as i64%(b-a)+a } #[inline] pub fn shuffle(list:&mut [T]){ for i in (0..list.len()).rev(){ list.swap(i,next()%(i+1)); } } } const N:usize=100; const M:usize=8; const A:i64=5; #[inline] fn get_dist(p1:(usize,usize),p2:(usize,usize))->i64{ let y=p1.0-p2.0; let x=p1.1-p2.1; (y*y+x*x) as i64 } struct In{ pos:[(usize,usize);N] } impl In{ #[inline] fn input()->In{ input!{ n:usize, m:usize, tp:[(usize,usize);N] } assert_eq!((n,m),(N,M)); let mut it=tp.into_iter(); In{pos:[();N].map(|_|it.next().unwrap())} } } struct Out{ m:[(usize,usize);M], dist:[[i64;N];N], cnt:[usize;N], route:Vec, log:Vec<(bool,usize)> // type,idx } impl Out{ #[inline] fn new(input:&In)->Out{ let mut route=(0..N).collect::>(); route.push(0); let mut cnt=[0;N]; for v in &route{ cnt[*v]+=1; } let m=[();M].map(|_|(rnd::next()%1001,rnd::next()%1001)); let mut dist=[[std::i64::MAX>>2;N];N]; for i in 0..N{ for j in 0..N{ dist[i][j]=A*A*get_dist(input.pos[i],input.pos[j]); } } for k in 0..N{ for i in 0..N{ for j in 0..N{ dist[i][j]=dist[i][j].min(dist[i][k]+dist[k][j]); } } } Out{m,dist,cnt,route,log:vec![]} } #[inline] fn score(&self,input:&In)->i64{ self.route.windows(2).map(|w|self.dist(input,w[0],w[1])).sum::() } #[inline] fn dist(&self,input:&In,a:usize,b:usize)->i64{ if a=N && b>=N{ get_dist(self.m[a-N],self.m[b-N]) } else if a>=N{ A*get_dist(self.m[a-N],input.pos[b]) } else{ A*get_dist(self.m[b-N],input.pos[a]) } } #[inline] fn sub(&self,input:&In,a:usize,p:(usize,usize))->i64{ if abool{ self.log.clear(); let mut prev=self.route[0]; let mut f=false; let mut at=1; let mut diff=0; for &v in &self.route[1..]{ if f{ diff+=self.dist(input,prev,v)-self.dist(input,prev,id)-self.dist(input,id,v); } if v!=id{ let d=self.sub(input,prev,new)+self.sub(input,v,new)-self.dist(input,prev,v); if 0>d{ diff+=d; self.log.push((true,at)); at+=1; } prev=v; at+=1; f=false; } else{ self.log.push((false,at)); f=true; } } if add>=diff{ *score+=diff; self.m[id-N]=new; for &(t,idx) in &self.log{ if t{ self.route.insert(idx,id); } else{ self.route.remove(idx); } } true } else{ false } } #[inline] fn try_opt2(&self,input:&In,a:usize,b:usize)->i64{ let a0=self.route[a-1]; let a1=self.route[a]; let b0=self.route[b-1]; let b1=self.route[b]; self.dist(input,a0,b0)+self.dist(input,a1,b1)-self.dist(input,a0,a1)-self.dist(input,b0,b1) } #[inline] fn opt2(&mut self,mut a:usize,mut b:usize){ if a>b{ std::mem::swap(&mut a,&mut b); } self.route[a..b].reverse(); } #[inline] fn try_insert(&self,input:&In,idx:usize,v:usize)->i64{ let v0=self.route[idx-1]; let v1=self.route[idx]; self.dist(input,v0,v)+self.dist(input,v,v1)-self.dist(input,v0,v1) } #[inline] fn insert(&mut self,idx:usize,v:usize){ self.route.insert(idx,v); if vi64{ let v0=self.route[idx-1]; let v1=self.route[idx]; let v2=self.route[idx+1]; self.dist(input,v0,v2)-self.dist(input,v0,v1)-self.dist(input,v1,v2) } #[inline] fn remove(&mut self,idx:usize){ let v=self.route.remove(idx); if v([(usize,usize);M],Vec){ let mut best=i64::MAX; let mut best_state=([(!0,!0);M],vec![]); let mut iter=0; let mut up=0; let mut th=[0;256]; const I:usize=5; for i in 0..I{ let start=get_time(); let tl=0.98*(i+1) as f64/I as f64; let mut cur=Out::new(input); let mut score=cur.score(input); loop{ if iter&1023==0{ let p=(get_time()-start)/(tl-start); if p>=1.{break;} const T0:f64=35000.; const T1:f64=0.; let heat=T0+(T1-T0)*p; th.fill_with(||(-heat*rnd::nextf().ln()) as i64); } let r=rnd::nextf(); const A:f64=0.5;// 2-opt const B:f64=0.2;// insert const C:f64=0.2;// remove // const D:f64=0.1;// change if r<=A{ let a=rnd::range(1,cur.route.len()-1); let mut b; while{ b=rnd::range(1,cur.route.len()-1); a-b+1<=2 }{} let diff=cur.try_opt2(input,a,b); if th[iter&255]>=diff{ score+=diff; cur.opt2(a,b); up+=1; } } else if r<=A+B{ let idx=rnd::range(1,cur.route.len()); let mut v; while{ v=rnd::next()%(N+M); v==cur.route[idx-1] || v==cur.route[idx] }{} let diff=cur.try_insert(input,idx,v); if th[iter&255]>=diff{ score+=diff; cur.insert(idx,v); up+=1; } } else if r<=A+B+C && cur.route.len()!=N+1{ let mut idx; let mut iter=0; while{ iter+=1; idx=rnd::range(1,cur.route.len()-1); let v=cur.route[idx]; v=diff{ score+=diff; cur.remove(idx); up+=1; } } else{ const R:i64=100; let id=rnd::next()&7; let p=cur.m[id]; let mut np; while{ np=(p.0+rnd::rangei(-R,R+1) as usize,p.1+rnd::rangei(-R,R+1) as usize); np.0>1000 || np.1>1000 }{} up+=cur.change(input,th[iter&255],id+N,np,&mut score) as usize; } iter+=1; if best>score{ best=score; best_state=(cur.m,cur.route.clone()); } } debug!(score); } let p=up as f64/iter as f64; debug!(iter,p); debug!(best); debug!(best_state.1.len()); best_state } fn main(){ let input=In::input(); let (m,route)=sa(&input); let mut dist=[[std::i64::MAX>>2;N+M];N+M]; let mut next=[[!0;N+M];N+M]; for i in 0..N+M{ let a=if i=N && j>=N{1} else{A}; next[i][j]=j; } } for k in 0..N+M{ for i in 0..N+M{ for j in 0..N+M{ if dist[i][j]>dist[i][k]+dist[k][j]{ next[i][j]=next[i][k]; dist[i][j]=dist[i][k]+dist[k][j]; } } } } let mut ans=vec![]; let mut ac=0; let mut seen=[true;N]; seen[0]=false; let mut prev=0; for v in route[1..route.len()-1].iter().cloned() .filter(move |&v|v