結果

問題 No.5020 Averaging
ユーザー nephrologistnephrologist
提出日時 2024-02-25 16:51:31
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 6,709 bytes
コンパイル時間 1,080 ms
コンパイル使用メモリ 188,040 KB
実行使用メモリ 40,548 KB
スコア 0
最終ジャッジ日時 2024-02-25 16:51:37
合計ジャッジ時間 5,626 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(unused_mut)]
#![allow(non_upper_case_globals)]
//proconio
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut 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::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

const mokuhyou: usize = 500000000000000000;
const n: usize = 45;
use std::cmp::Ordering;
use std::cmp::{Eq, Ord, PartialEq, PartialOrd};
use std::collections::BinaryHeap;
const WID: usize = 100;
const CHA: usize = 50;

pub fn get_time() -> f64 {
    static mut STIME: f64 = -1.0; // 初期値
    let t = std::time::SystemTime::now() // nowを取得
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap();
    // a.duration_since(b)だとa>bが前提
    let ms = t.as_secs() as f64 + t.subsec_nanos() as f64 * 1e-9;
    // as_secsが秒, .subsec_nanos()が小数点以下の秒
    unsafe {
        if STIME < 0.0 {
            STIME = ms; // 経過時間の初期化
        }
        // ローカル環境とジャッジ環境の実行速度差はget_timeで吸収しておくと便利
        #[cfg(feature = "local")]
        {
            (ms - STIME) * 1.3
        }
        #[cfg(not(feature = "local"))]
        {
            ms - STIME
        }
    }
}
fn main() {
    get_time();
    input! {nn:usize, XY:[(usize,usize);n]}
    let inp = Input { XY };
    let mut rng = Xorshift::new(20250225);
    // eprintln!("XY {:?}", &inp.XY);
    let bestboard = beam_search(&inp, &mut rng, WID, CHA);
    bestboard.printstate();
    eprintln!(
        "{}",
        bestboard.nowval[0].0.abs_diff(mokuhyou) + bestboard.nowval[0].1.abs_diff(mokuhyou)
    );
    eprintln!("time {}", get_time());
}

struct Xorshift {
    val: usize,
}
impl Xorshift {
    fn new(seed: usize) -> Self {
        let mut hoge = seed;
        hoge ^= seed << 17;
        hoge ^= seed >> 13;
        hoge ^= seed << 5;
        Xorshift { val: hoge }
    }
    fn next(&mut self) {
        let hoge = self.val;
        self.val ^= hoge << 17;
        self.val ^= hoge >> 13;
        self.val ^= hoge << 5;
    }
    fn rand(&mut self) -> f64 {
        self.next();
        (self.val / std::usize::MAX) as f64
    }
    fn randint(&mut self, a: usize, b: usize) -> usize {
        self.next();
        self.val % (b - a)
    }
}
struct Input {
    XY: Vec<(usize, usize)>,
}
#[derive(Clone)]
// #[derive(Debug, PartialEq, PartialOrd, Eq)]
struct Board {
    orders: Vec<(usize, usize)>,
    nowval: Vec<(usize, usize)>,
    goukei: usize,
    nijou: usize,
    cost: f64,
}

impl Board {
    fn new(inp: &Input) -> Self {
        let mut goukei = 0;
        let mut nijou = 0;
        for &(a, b) in inp.XY.iter() {
            goukei += a + b;
            nijou += a * a + b * b;
        }

        Self {
            orders: vec![],
            nowval: inp.XY.clone(),
            goukei,
            nijou,
            cost: 0.0,
        }
    }
    fn action(&mut self, a: usize, b: usize) {
        let n1 = self.nowval[a].0;
        let n2 = self.nowval[a].1;
        let n3 = self.nowval[b].0;
        let n4 = self.nowval[b].1;
        let v1 = (n1 + n3) / 2;
        let v2 = (n2 + n4) / 2;
        self.nowval[a] = (v1, v2);
        self.nowval[b] = (v1, v2);
        self.orders.push((a + 1, b + 1));
    }
    fn calccost(&mut self) {
        let std = (self.nijou / n) as f64
            - (self.goukei as f64 / n as f64) * (self.goukei as f64 / n as f64);
        self.cost = (mokuhyou.abs_diff(self.nowval[0].0) + mokuhyou.abs_diff(self.nowval[0].1))
            as f64
            - (std * 100.0);
    }
    fn printstate(&self) {
        println!("{}", self.orders.len());
        for &(a, b) in self.orders.iter() {
            println!("{} {}", a, b);
        }
    }
}
// 順序関係を作る。

impl Ord for Board {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap_or(Ordering::Equal)
    }
}

impl PartialEq for Board {
    fn eq(&self, other: &Self) -> bool {
        self.cost == other.cost
    }
}
impl Eq for Board {} // ここは空でOK
impl PartialOrd for Board {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.cost.partial_cmp(&other.cost)
    }
}

fn beam_search(inp: &Input, rng: &mut Xorshift, beam_width: usize, challenge_time: usize) -> Board {
    let board = Board::new(inp);
    let mut preheap = BinaryHeap::new();
    let mut nexheap = BinaryHeap::new();
    let mut bestboard = board.clone();
    preheap.push(board);
    for _ in 0..50 {
        for __ in 0..beam_width {
            if preheap.is_empty() {
                break;
            }
            let mut preboard = preheap.pop().unwrap();
            for ___ in 0..challenge_time {
                let mut nexboard = preboard.clone();
                let idx1 = rng.randint(0, n - 1);
                let idx2 = rng.randint(idx1, n);
                nexboard.action(idx1, idx2);
                nexheap.push(nexboard);
                let mut nexboard = preboard.clone();
                let idx1 = rng.randint(0, n);
                nexboard.action(0, idx1);
                nexheap.push(nexboard);
            }
            preheap.clear();
            for _ in 0..beam_width {
                if nexheap.is_empty() {
                    break;
                }
                let mut board = nexheap.pop().unwrap();
                if board.nowval[0].0.abs_diff(mokuhyou) < bestboard.nowval[0].0.abs_diff(mokuhyou) {
                    bestboard = board.clone();
                }
                preheap.push(board);
            }
            nexheap.clear()
        }
    }

    bestboard
}
0