結果

問題 No.5008 [Cherry Alpha] Discrete Pendulum with Air Resistance
ユーザー terry_u16terry_u16
提出日時 2022-10-14 23:25:29
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 11,580 bytes
コンパイル時間 1,308 ms
実行使用メモリ 6,956 KB
スコア 0
最終ジャッジ日時 2022-10-14 23:27:16
合計ジャッジ時間 104,452 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `time::Instant`
 --> Main.rs:1:25
  |
1 | use std::{fmt::Display, time::Instant};
  |                         ^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: 1 warning emitted

ソースコード

diff #

use std::{fmt::Display, time::Instant};

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

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

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

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

#[allow(unused_macros)]
macro_rules! chmin {
    ($base:expr, $($cmps:expr),+ $(,)*) => {{
        let cmp_min = min!($($cmps),+);
        if $base > cmp_min {
            $base = cmp_min;
            true
        } else {
            false
        }
    }};
}

#[allow(unused_macros)]
macro_rules! chmax {
    ($base:expr, $($cmps:expr),+ $(,)*) => {{
        let cmp_max = max!($($cmps),+);
        if $base < cmp_max {
            $base = cmp_max;
            true
        } else {
            false
        }
    }};
}

#[allow(unused_macros)]
macro_rules! min {
    ($a:expr $(,)*) => {{
        $a
    }};
    ($a:expr, $b:expr $(,)*) => {{
        std::cmp::min($a, $b)
    }};
    ($a:expr, $($rest:expr),+ $(,)*) => {{
        std::cmp::min($a, min!($($rest),+))
    }};
}

#[allow(unused_macros)]
macro_rules! max {
    ($a:expr $(,)*) => {{
        $a
    }};
    ($a:expr, $b:expr $(,)*) => {{
        std::cmp::max($a, $b)
    }};
    ($a:expr, $($rest:expr),+ $(,)*) => {{
        std::cmp::max($a, max!($($rest),+))
    }};
}

#[allow(unused_macros)]
macro_rules! mat {
    ($e:expr; $d:expr) => { vec![$e; $d] };
    ($e:expr; $d:expr $(; $ds:expr)+) => { vec![mat![$e $(; $ds)*]; $d] };
}

#[allow(unused_macros)]
macro_rules! skip_none {
    ($res:expr) => {
        if let Some(v) = $res {
            v
        } else {
            continue;
        }
    };
}

#[derive(Debug, Clone)]
struct Input {
    n: usize,
    _k: usize,
    oppose_times: Vec<i64>,
    coop_times: Vec<i64>,
}

impl Input {
    fn read_input() -> Self {
        input! {
            n: usize,
            k: usize,
            oppose_times: [i64; k],
            coop_times: [i64; k]
        }

        Self {
            n,
            _k: k,
            oppose_times,
            coop_times,
        }
    }
}

#[derive(Debug, Clone)]
struct State {
    pendulums: Vec<Pendulum>,
}

impl State {
    fn calc_score_all(&self, input: &Input) -> i64 {
        let mut oppose_x = Vec::with_capacity(self.pendulums.len());
        let mut coop_x = Vec::with_capacity(self.pendulums.len());

        for p in self.pendulums.iter() {
            let op = p.get_all_x(&input.oppose_times);
            let co = p.get_all_x(&input.coop_times);
            oppose_x.push(op);
            coop_x.push(co);
        }

        let mut oppose_sum = 0.0;
        let mut coop_sum = 0.0;

        for i in 0..oppose_x.len() {
            let xi = &oppose_x[i];
            let pi = &self.pendulums[i];

            for j in (i + 1)..oppose_x.len() {
                let xj = &oppose_x[j];
                let pj = &self.pendulums[j];

                for (x, y) in xi.iter().zip(xj) {
                    oppose_sum += (x - y).abs() as f64 / (pi.init + pj.init) as f64;
                }
            }
        }

        let mut max = vec![std::i64::MIN; coop_x.len()];
        let mut min = vec![std::i64::MAX; coop_x.len()];

        for x in coop_x.iter() {
            for (i, &x) in x.iter().enumerate() {
                chmax!(max[i], x);
                chmin!(min[i], x);
            }
        }

        for i in 0..coop_x.len() {
            coop_sum += ((max[i] - min[i]) as f64 * 0.05 + 1.0).sqrt();
        }

        let oppose_score = oppose_sum * 2e7 / (input.n * (input.n - 1)) as f64;
        let coop_score = 1e7 / coop_sum;

        (oppose_score * coop_score).round() as i64
    }
}

impl Display for State {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{} {} {}",
            self.pendulums[0].init, self.pendulums[0].min, self.pendulums[0].damper
        )?;

        for p in self.pendulums[1..].iter() {
            writeln!(f)?;
            write!(f, "{} {} {}", p.init, p.min, p.damper)?;
        }

        Ok(())
    }
}

#[derive(Debug, Clone, Copy)]
struct Pendulum {
    init: i64,
    min: i64,
    damper: i64,
}

impl Pendulum {
    fn new(init: i64, min: i64, damper: i64) -> Self {
        Self { init, min, damper }
    }

    fn get_x(&self, time: i64) -> i64 {
        // 振幅がmin以下になる時間
        let min_round = (self.init - self.min) / self.damper;
        let init_period = self.get_period(0);
        let last_period = self.get_period(min_round);
        let min_t = min_round * (init_period + last_period) / 2;

        let round = if time >= min_t {
            min_round
        } else {
            binary_search(
                &|round| {
                    let last_period = self.get_period(round);
                    let t = min_round * (init_period + last_period) / 2;
                    t <= time
                },
                0,
                (time + init_period - 1) / init_period + 1,
            )
        };

        let round_period = self.get_period(round);
        let mut t = round * (init_period + round_period) / 2;
        let mut amp = self.get_amp(round);
        let mut dir = 1;

        loop {
            let mut remain = time - t;

            if amp == self.min {
                remain %= 2 * amp;
            }

            if remain <= 2 * amp {
                if remain <= amp {
                    return dir * remain;
                } else {
                    return dir * (2 * amp - remain);
                }
            }

            t += 2 * amp;
            amp -= self.damper;
            amp = amp.max(self.min);
            dir = -dir;
        }
    }

    fn get_all_x(&self, times: &[i64]) -> Vec<i64> {
        let mut x = Vec::with_capacity(times.len());

        for &t in times {
            x.push(self.get_x(t));
        }

        x
    }

    fn get_amp(&self, round: i64) -> i64 {
        (self.init - self.damper * round * 2).max(self.min)
    }

    fn get_period(&self, round: i64) -> i64 {
        self.get_amp(round) * 4 - 4
    }
}

fn main() {
    let input = Input::read_input();
    let state = get_init_state(&input);
    let state = annealing(&input, state, 1.9);

    eprintln!("{}", state.calc_score_all(&input));
    println!("{}", state);
}

fn annealing(input: &Input, initial_solution: State, duration: f64) -> State {
    let mut solution = initial_solution;
    let mut best_solution = solution.clone();
    let mut current_score = solution.calc_score_all(input);
    let mut best_score = current_score;
    let init_score = current_score;

    let mut all_iter = 0;
    let mut valid_iter = 0;
    let mut accepted_count = 0;
    let mut update_count = 0;
    let mut rng = Xorshift::new(42);

    let duration_inv = 1.0 / duration;
    let since = std::time::Instant::now();

    let temp0 = 1e12;
    let temp1 = 1e10;

    loop {
        all_iter += 1;

        let time = (std::time::Instant::now() - since).as_secs_f64() * duration_inv;
        if time >= 1.0 {
            break;
        }
        let temp = f64::powf(temp0, 1.0 - time) * f64::powf(temp1, time);

        // 変形
        let index = rng.rand(input.n as u64) as usize;
        let sign = rng.rand(2) as i64 * 2 - 1;
        let change_type = rng.rand(3);
        let mut new_state = solution.clone();

        if change_type == 1 {
            let new_init = new_state.pendulums[index].init + sign;

            if new_init <= 0 || new_init > 1_000_000_000 {
                continue;
            }

            new_state.pendulums[index].init = new_init;
        } else if change_type == 2 {
            let new_min = new_state.pendulums[index].min + sign;

            if new_min <= 0 || new_min > new_state.pendulums[index].init {
                continue;
            }

            new_state.pendulums[index].min = new_min;
        } else {
            let damper = new_state.pendulums[index].damper + sign;

            if damper <= 0
                || damper > new_state.pendulums[index].init - new_state.pendulums[index].min
            {
                continue;
            }

            new_state.pendulums[index].damper = damper;
        };

        // スコア計算
        let new_score = new_state.calc_score_all(input);
        let score_diff = new_score - current_score;

        if score_diff >= 0 || rng.randf() < f64::exp(score_diff as f64 / temp) {
            // 解の更新
            current_score = new_score;
            solution = new_state;
            accepted_count += 1;

            if chmax!(best_score, current_score) {
                best_solution = solution.clone();
                update_count += 1;
            }
        }

        valid_iter += 1;
    }

    eprintln!("===== annealing =====");
    eprintln!("init score : {}", init_score);
    eprintln!("score      : {}", best_score);
    eprintln!("all iter   : {}", all_iter);
    eprintln!("valid iter : {}", valid_iter);
    eprintln!("accepted   : {}", accepted_count);
    eprintln!("updated    : {}", update_count);
    eprintln!("");

    best_solution
}

fn get_init_state(input: &Input) -> State {
    let mut pendulums = vec![];

    for i in 0..input.n {
        let j = (i % 2 + 1) as i64;
        pendulums.push(Pendulum::new(j, j, j));
    }

    State { pendulums }
}

fn binary_search<F: Fn(i64) -> bool>(f: &F, mut ok: i64, mut ng: i64) -> i64 {
    while (ok - ng).abs() > 1 {
        let mid = (ok + ng) / 2;

        if f(mid) {
            ok = mid;
        } else {
            ng = mid;
        }
    }

    ok
}

#[derive(Debug)]
#[allow(dead_code)]
pub struct Xorshift {
    seed: u64,
}

#[allow(dead_code)]
impl Xorshift {
    pub fn new(seed: u64) -> Xorshift {
        Xorshift { seed }
    }

    #[inline]
    pub fn next(&mut self) -> u64 {
        self.seed = self.seed ^ (self.seed << 13);
        self.seed = self.seed ^ (self.seed >> 7);
        self.seed = self.seed ^ (self.seed << 17);
        self.seed
    }

    #[inline]
    pub fn rand(&mut self, m: u64) -> u64 {
        self.next() % m
    }

    #[inline]
    // 0.0 ~ 1.0
    pub fn randf(&mut self) -> f64 {
        use std::mem;
        const UPPER_MASK: u64 = 0x3FF0000000000000;
        const LOWER_MASK: u64 = 0xFFFFFFFFFFFFF;
        let tmp = UPPER_MASK | (self.next() & LOWER_MASK);
        let result: f64 = unsafe { mem::transmute(tmp) };
        result - 1.0
    }
}
0