結果

問題 No.5010 Better Mo's Algorithm is Needed!! (Unweighted)
ユーザー rhoorhoo
提出日時 2022-12-17 01:19:40
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 6,688 bytes
コンパイル時間 4,416 ms
実行使用メモリ 13,780 KB
スコア 0
最終ジャッジ日時 2022-12-17 01:30:34
合計ジャッジ時間 647,411 ms
ジャッジサーバーID
(参考情報)
judge16 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
testcase_71 WA -
testcase_72 WA -
testcase_73 WA -
testcase_74 WA -
testcase_75 WA -
testcase_76 WA -
testcase_77 WA -
testcase_78 WA -
testcase_79 WA -
testcase_80 WA -
testcase_81 WA -
testcase_82 WA -
testcase_83 WA -
testcase_84 WA -
testcase_85 WA -
testcase_86 WA -
testcase_87 WA -
testcase_88 WA -
testcase_89 WA -
testcase_90 WA -
testcase_91 WA -
testcase_92 WA -
testcase_93 WA -
testcase_94 WA -
testcase_95 WA -
testcase_96 WA -
testcase_97 WA -
testcase_98 WA -
testcase_99 WA -
testcase_100 WA -
testcase_101 WA -
testcase_102 WA -
testcase_103 WA -
testcase_104 WA -
testcase_105 WA -
testcase_106 WA -
testcase_107 WA -
testcase_108 WA -
testcase_109 WA -
testcase_110 WA -
testcase_111 WA -
testcase_112 WA -
testcase_113 WA -
testcase_114 WA -
testcase_115 WA -
testcase_116 WA -
testcase_117 WA -
testcase_118 WA -
testcase_119 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused macro definition: `debug`
  --> Main.rs:88:14
   |
88 | macro_rules! debug{
   |              ^^^^^
   |
   = note: `#[warn(unused_macros)]` on by default

warning: unused macro definition: `param`
   --> Main.rs:152:14
    |
152 | macro_rules! param{
    |              ^^^^^

warning: unused variable: `p`
   --> Main.rs:318:9
    |
318 |     let p=up as f64/iter as f64;
    |         ^ help: if this is intentional, prefix it with an underscore: `_p`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: field is never read: `st`
   --> Main.rs:217:5
    |
217 |     st:usize,
    |     ^^^^^^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: associated function is never used: `score`
   --> Main.rs:256:8
    |
256 |     fn score(&self)->i64{
    |        ^^^^^

warning: 5 warnings emitted

ソースコード

diff #

#![allow(non_snake_case)]


#[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.
        }
    }
    
    #[inline]
    pub fn range(a:usize,b:usize)->usize{
        next()%(b-a)+a
    }
    
    #[inline]
    pub fn exu(a:usize,b:usize,skip:usize)->usize{
        let ret=range(a,b-1);
        if ret==skip{b-1}
        else{ret}
    }

    #[inline]
    pub fn shuffle<T>(list:&mut [T]){
        for i in (0..list.len()).rev(){
            list.swap(i,next()%(i+1));
        }
    }
}


#[inline]
fn get_time_secs()->f64{
    std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs_f64()
}


struct Timer(f64);
impl Timer{
    #[inline]
    fn new()->Timer{
        Timer(get_time_secs())
    }

    #[inline]
    fn get_time(&self)->f64{
        get_time_secs()-self.0
    }
}


#[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))]
macro_rules! debug{
    ($($_:tt)*)=>{}
}


#[cfg(local)]
macro_rules! log{
    ()=>{};

    ($x:literal)=>{
        eprintln!("{}",$x);
    };
    
    ($x:ident $(,)?)=>{
        log!(@out $x,$x);
    };
    ($x:ident,$t:ident $(,)?)=>{
        log!(@out $x,$x);
        log!($t);
    };
    ($x:ident,$($_:ident).* $(,)?)=>{
        compile_error!();
    };
    ($x:ident,$t:expr $(,)?)=>{
        log!(@out $x,$t);
    };
    ($($x:ident).* $(,)?)=>{
        log!(@last $($x).*;$($x).*);
    };

    ($x:ident,$t:ident,$($rest:tt)*)=>{
        log!(@out $x,$x);
        log!($t,$($rest)*);
    };
    ($x:ident,$($_:ident).*,$($rest:tt)*)=>{
        compile_error!();
    };
    ($x:ident,$t:expr,$($rest:tt)*)=>{
        log!(@out $x,$t);
        log!($($rest)*);
    };
    ($($x:ident).*,$($rest:tt)*)=>{
        log!(@last $($x).*;$($x).*);
        log!($($rest)*);
    };

    (@out $x:ident,$t:expr)=>{
        eprintln!("{} = {:?}",stringify!($x),$t);
    };

    (@last $x:ident;$full:expr)=>{
        log!(@out $x,$full);
    };
    (@last $_:ident. $($rest:ident).*;$full:expr)=>{
        log!(@last $($rest).*;$full)
    }
}

#[cfg(not(local))]
macro_rules! log{
    ($($_:tt)*)=>{}
}


macro_rules! param{
    ($($x:ident:$t:ty=$v:literal),* $(,)?)=>{
        #[allow(non_snake_case)]
        struct Param{
            $($x:$t),*
        }
        impl Param{
            #[inline]
            fn new()->Self{
                Self{
                    $(
                        $x:match std::env::var(stringify!($x)){
                            Ok(s)=>s.parse().expect("parse error"),
                            Err(_)=>$v
                        }
                    ),*
                }
            }
        }
    };
}


#[allow(unused)]
struct Scanner{
    stack:std::str::SplitAsciiWhitespace<'static>
}
#[allow(unused)]
impl Scanner{
    #[inline]
    fn new()->Self{
        Self{stack:"".split_ascii_whitespace()}
    }

    #[inline]
    fn read<T:std::str::FromStr>(&mut self)->T{
        loop{
            if let Some(v)=self.stack.next(){
                return v.parse::<T>().unwrap_or_else(|_|panic!("{}: parse error",std::any::type_name::<T>()));
            }

            let mut tmp=String::new();
            std::io::stdin().read_line(&mut tmp).unwrap();
            assert!(!tmp.is_empty());
            self.stack=Box::leak(tmp.into_boxed_str()).split_ascii_whitespace();
        }
    }
}


use std::io::*;
use std::mem;


const N:usize=200000;
const Q:usize=200000;


#[inline]
fn abs_diff(a:usize,b:usize)->usize{
    (a as i64-b as i64).abs() as usize
}


struct Out{
    st:usize,
    route:Vec<(usize,usize,usize)>
}
impl Out{
    #[inline]
    fn input()->Out{
        let mut scan=Scanner::new();
        let n:usize=scan.read();
        let q:usize=scan.read();
        let wt:usize=scan.read();
        let st:usize=scan.read();
        assert_eq!((n,q,wt),(N,Q,1));
        for _ in 0..n{
            scan.read::<usize>();
        }

        let mut route=Vec::with_capacity(Q);
        let mut min_dist=!0;
        let mut arg_min=!0;
        for i in 0..Q{
            let l:usize=scan.read();
            let r:usize=scan.read();
            route.push((l,r,i));
            let dist=r-l;
            if min_dist>dist{
                min_dist=dist;
                arg_min=i;
            }
        }
        route.swap(0,arg_min);

        route[1..].sort_unstable_by_key(|(l,r,_)|{
            (l>>9,r>>9)
        });
        
        Out{st,route}
    }

    #[inline]
    fn score(&self)->i64{
        let mut ret=self.route[0].1-self.route[0].0;

        for i in 1..self.route.len(){
            ret+=self.dist(i-1,i);
        }
        
        ret as i64
    }

    #[inline]
    fn dist(&self,a:usize,b:usize)->usize{
        let (l0,r0,_)=self.route[a];
        let (l1,r1,_)=self.route[b];

        abs_diff(l0,l1)+abs_diff(r0,r1)
    }

    #[inline]
    fn try_2opt(&self,a:usize,b:usize)->i64{
        (self.dist(a-1,b-1)+self.dist(a,b)-self.dist(a-1,a)-self.dist(b-1,b)) as i64
    }

    #[inline]
    fn apply_2opt(&mut self,mut a:usize,mut b:usize){
        if a>b{
            mem::swap(&mut a,&mut b);
        }

        self.route[a..b].reverse()
    }
}


fn hc(out:&mut Out,time:&Timer){
    let mut score=0;
    let mut best=score;

    let mut iter=0;
    let mut up=0;

    loop{
        if iter&1023==0{
            const TL:f64=4.98;
            let p=time.get_time()/TL;
            if p>=1.{break;}
        }
        iter+=1;

        let a=rnd::range(1,Q);
        let b=rnd::range(1,Q);

        let diff=out.try_2opt(a,b);

        if diff<=0{
            out.apply_2opt(a,b);
            score+=diff;
            up+=1;
            best=best.min(score);
        }
    }

    let p=up as f64/iter as f64;
    log!(score,best,iter,p);

    log!(scoref,out.score());
}


fn main(){
    let time=Timer::new();
    let mut out=Out::input();

    hc(&mut out,&time);

    let stdout=stdout();
    let stdout=&mut BufWriter::new(stdout.lock());
    for (_,_,id) in &out.route{
        writeln!(stdout,"{id}").unwrap();
    }
    stdout.flush().unwrap();
}
0