結果

問題 No.5017 Tool-assisted Shooting
ユーザー tipstar0125tipstar0125
提出日時 2023-08-07 18:42:36
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 814 ms / 2,000 ms
コード長 11,089 bytes
コンパイル時間 4,403 ms
コンパイル使用メモリ 166,092 KB
実行使用メモリ 24,524 KB
スコア 41,829
平均クエリ数 379.18
最終ジャッジ日時 2023-08-07 18:43:31
合計ジャッジ時間 55,013 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 568 ms
24,036 KB
testcase_01 AC 423 ms
24,408 KB
testcase_02 AC 345 ms
24,372 KB
testcase_03 AC 367 ms
24,024 KB
testcase_04 AC 275 ms
23,436 KB
testcase_05 AC 248 ms
23,832 KB
testcase_06 AC 430 ms
23,352 KB
testcase_07 AC 338 ms
23,436 KB
testcase_08 AC 406 ms
23,376 KB
testcase_09 AC 687 ms
23,900 KB
testcase_10 AC 451 ms
23,424 KB
testcase_11 AC 516 ms
24,048 KB
testcase_12 AC 212 ms
23,832 KB
testcase_13 AC 482 ms
24,300 KB
testcase_14 AC 237 ms
23,616 KB
testcase_15 AC 203 ms
23,424 KB
testcase_16 AC 254 ms
23,424 KB
testcase_17 AC 537 ms
24,408 KB
testcase_18 AC 557 ms
24,012 KB
testcase_19 AC 328 ms
23,544 KB
testcase_20 AC 356 ms
23,892 KB
testcase_21 AC 675 ms
23,412 KB
testcase_22 AC 340 ms
23,520 KB
testcase_23 AC 346 ms
24,024 KB
testcase_24 AC 144 ms
23,364 KB
testcase_25 AC 481 ms
24,492 KB
testcase_26 AC 433 ms
23,376 KB
testcase_27 AC 514 ms
24,336 KB
testcase_28 AC 775 ms
24,024 KB
testcase_29 AC 415 ms
23,628 KB
testcase_30 AC 237 ms
23,520 KB
testcase_31 AC 562 ms
23,400 KB
testcase_32 AC 408 ms
23,520 KB
testcase_33 AC 371 ms
24,276 KB
testcase_34 AC 212 ms
23,628 KB
testcase_35 AC 481 ms
24,048 KB
testcase_36 AC 237 ms
24,036 KB
testcase_37 AC 457 ms
24,348 KB
testcase_38 AC 446 ms
24,348 KB
testcase_39 AC 488 ms
23,652 KB
testcase_40 AC 538 ms
23,364 KB
testcase_41 AC 404 ms
24,276 KB
testcase_42 AC 573 ms
23,388 KB
testcase_43 AC 255 ms
23,364 KB
testcase_44 AC 541 ms
24,048 KB
testcase_45 AC 499 ms
24,024 KB
testcase_46 AC 592 ms
23,652 KB
testcase_47 AC 531 ms
24,024 KB
testcase_48 AC 571 ms
24,372 KB
testcase_49 AC 407 ms
23,628 KB
testcase_50 AC 513 ms
23,832 KB
testcase_51 AC 324 ms
23,424 KB
testcase_52 AC 456 ms
24,024 KB
testcase_53 AC 321 ms
24,012 KB
testcase_54 AC 551 ms
24,036 KB
testcase_55 AC 466 ms
24,524 KB
testcase_56 AC 551 ms
23,376 KB
testcase_57 AC 704 ms
23,424 KB
testcase_58 AC 186 ms
24,360 KB
testcase_59 AC 423 ms
24,012 KB
testcase_60 AC 273 ms
23,400 KB
testcase_61 AC 456 ms
23,400 KB
testcase_62 AC 494 ms
23,640 KB
testcase_63 AC 491 ms
23,532 KB
testcase_64 AC 631 ms
24,012 KB
testcase_65 AC 225 ms
24,036 KB
testcase_66 AC 640 ms
23,424 KB
testcase_67 AC 563 ms
24,372 KB
testcase_68 AC 289 ms
23,364 KB
testcase_69 AC 388 ms
24,264 KB
testcase_70 AC 418 ms
23,640 KB
testcase_71 AC 486 ms
23,532 KB
testcase_72 AC 478 ms
23,400 KB
testcase_73 AC 299 ms
23,832 KB
testcase_74 AC 368 ms
23,832 KB
testcase_75 AC 477 ms
23,616 KB
testcase_76 AC 362 ms
24,060 KB
testcase_77 AC 559 ms
23,400 KB
testcase_78 AC 429 ms
24,264 KB
testcase_79 AC 592 ms
24,384 KB
testcase_80 AC 677 ms
23,388 KB
testcase_81 AC 647 ms
23,376 KB
testcase_82 AC 553 ms
24,036 KB
testcase_83 AC 302 ms
24,324 KB
testcase_84 AC 425 ms
23,400 KB
testcase_85 AC 455 ms
24,064 KB
testcase_86 AC 554 ms
23,804 KB
testcase_87 AC 464 ms
23,520 KB
testcase_88 AC 814 ms
24,020 KB
testcase_89 AC 232 ms
24,336 KB
testcase_90 AC 209 ms
23,820 KB
testcase_91 AC 215 ms
23,904 KB
testcase_92 AC 390 ms
23,832 KB
testcase_93 AC 421 ms
23,376 KB
testcase_94 AC 159 ms
23,376 KB
testcase_95 AC 397 ms
23,436 KB
testcase_96 AC 297 ms
24,036 KB
testcase_97 AC 438 ms
23,400 KB
testcase_98 AC 437 ms
23,424 KB
testcase_99 AC 383 ms
23,616 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: variable `turn` is assigned to, but never used
   --> Main.rs:323:13
    |
323 |     let mut turn = 0;
    |             ^^^^
    |
    = note: consider using `_turn` instead
    = note: `#[warn(unused_variables)]` on by default

warning: value assigned to `turn` is never read
   --> Main.rs:327:9
    |
327 |         turn = t + 1;
    |         ^^^^
    |
    = help: maybe it is overwritten before being read?
    = note: `#[warn(unused_assignments)]` on by default

warning: 2 warnings emitted

ソースコード

diff #

#![allow(non_snake_case)]
#![allow(unused_imports)]
#![allow(unused_macros)]
#![allow(clippy::comparison_chain)]
#![allow(clippy::nonminimal_bool)]
#![allow(clippy::neg_multiply)]
#![allow(clippy::type_complexity)]
#![allow(clippy::needless_range_loop)]
#![allow(dead_code)]

use std::{
    cmp::Reverse,
    collections::{BTreeMap, BTreeSet, BinaryHeap, VecDeque},
};

mod rnd {
    static mut S: usize = 0;
    static MAX: usize = 1e9 as usize;

    #[inline]
    pub fn init(seed: usize) {
        unsafe {
            if seed == 0 {
                let t = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap()
                    .as_secs() as usize;
                S = t
            } else {
                S = seed;
            }
        }
    }
    #[inline]
    pub fn gen() -> usize {
        unsafe {
            if S == 0 {
                init(0);
            }
            S ^= S << 7;
            S ^= S >> 9;
            S
        }
    }
    #[inline]
    pub fn gen_range(a: usize, b: usize) -> usize {
        gen() % (b - a) + a
    }
    #[inline]
    pub fn gen_bool() -> bool {
        gen() & 1 == 1
    }
    #[inline]
    pub fn gen_range_isize(a: usize) -> isize {
        let mut x = (gen() % a) as isize;
        if gen_bool() {
            x *= -1;
        }
        x
    }
    #[inline]
    pub fn gen_range_neg_wrapping(a: usize) -> usize {
        let mut x = gen() % a;
        if gen_bool() {
            x = x.wrapping_neg();
        }
        x
    }
    #[inline]
    pub fn gen_float() -> f64 {
        ((gen() % MAX) as f64) / MAX as f64
    }
}

#[derive(Debug, Clone)]
struct TimeKeeper {
    start_time: std::time::Instant,
    time_threshold: f64,
}

impl TimeKeeper {
    fn new(time_threshold: f64) -> Self {
        TimeKeeper {
            start_time: std::time::Instant::now(),
            time_threshold,
        }
    }
    #[inline]
    fn isTimeOver(&self) -> bool {
        let elapsed_time = self.start_time.elapsed().as_nanos() as f64 * 1e-9;
        #[cfg(feature = "local")]
        {
            elapsed_time * 1.5 >= self.time_threshold
        }
        #[cfg(not(feature = "local"))]
        {
            elapsed_time >= self.time_threshold
        }
    }
    #[inline]
    fn get_time(&self) -> f64 {
        let elapsed_time = self.start_time.elapsed().as_nanos() as f64 * 1e-9;
        #[cfg(feature = "local")]
        {
            elapsed_time * 1.5
        }
        #[cfg(not(feature = "local"))]
        {
            elapsed_time
        }
    }
}

const H: usize = 60;
const W: usize = 25;
const TURN: usize = 1000;
const INF: isize = 1 << 60;
const TARGET_LEVEL: isize = 250;

#[derive(Debug, Clone, Eq, PartialEq)]
struct Player {
    x: usize,
    y: usize,
}

impl Player {
    fn new() -> Self {
        Player { x: 12, y: 0 }
    }
    fn move_(&mut self, action: isize) {
        self.y += 1;
        self.x = (W as isize + action + self.x as isize) as usize % W;
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
struct Enemy {
    hp: isize,
    power: isize,
    init_hp: isize,
    x: usize,
    y: usize,
}

impl Enemy {
    fn new(init_hp: isize, power: isize, x: usize, y: usize) -> Self {
        Enemy {
            hp: init_hp,
            power,
            init_hp,
            x,
            y,
        }
    }
}

#[derive(Debug, Clone)]
struct Field {
    enemies: Vec<Vec<Option<Enemy>>>,
}

impl Field {
    fn new() -> Self {
        Field {
            enemies: vec![vec![None; W]; H + TURN + 10],
        }
    }
    fn update(&mut self, n: usize, turn: usize) {
        let y = H + turn;
        for _ in 0..n {
            let v: Vec<isize> = read_vec();
            let h = v[0];
            let p = v[1];
            let x = v[2] as usize;
            let enemy = Enemy::new(h, p, x, y);
            self.enemies[y][x] = Some(enemy);
        }
    }
}

#[derive(Debug, Clone, Eq)]
struct State {
    player: Player,
    S: isize,
    score: isize,
    damage: isize,
    enemies: [Option<Enemy>; W],
    evaluated_score: isize,
    turn: usize,
    first_action: isize,
    is_dead: bool,
}

impl State {
    fn new() -> Self {
        State {
            player: Player::new(),
            S: 0,
            score: 0,
            damage: 0,
            enemies: [None; W],
            evaluated_score: 0,
            turn: 0,
            first_action: 0,
            is_dead: false,
        }
    }
    fn update_enemy(&mut self, field: &Field) {
        for x in 0..W {
            if let Some(enemy) = self.enemies[x] {
                if enemy.y == self.turn {
                    self.enemies[x] = None;
                }
            }
            if self.enemies[x].is_none() {
                for y in self.turn + 1..self.turn + H {
                    if let Some(enemy) = field.enemies[y][x] {
                        self.enemies[x] = Some(enemy);
                        break;
                    }
                }
            }
        }
    }
    fn advance(&mut self, action: isize) {
        self.player.move_(action);
        self.attack();
        self.turn += 1;
    }
    fn attack(&mut self) {
        if let Some(mut enemy) = self.enemies[self.player.x] {
            let level = self.get_level();
            if enemy.y == self.player.y {
                self.is_dead = true;
                return;
            }
            if enemy.y == self.player.y + 1 && enemy.hp > level {
                self.is_dead = true;
                return;
            }
            if enemy.hp <= level {
                self.score += enemy.init_hp;
                self.S += enemy.power;
                self.damage += min!(level, enemy.hp);
                self.enemies[self.player.x] = None;
            } else {
                enemy.hp -= level;
                self.enemies[self.player.x] = Some(enemy);
                self.damage += level;
            }
        }
    }
    fn evaluate_score(&mut self) {
        let level = self.get_level();
        self.evaluated_score = self.S * 1e6 as isize + self.damage;

        if let Some(enemy) = self.enemies[self.player.x] {
            let turn = enemy.y - self.player.y;
            if enemy.hp <= level * turn as isize {
                self.evaluated_score += 1000;
            }
        }
    }
    fn get_level(&self) -> isize {
        1 + self.S / 100
    }
    fn is_done(&self) -> bool {
        self.turn == TURN
    }
    fn output(&self, action: isize) {
        if action == 0 {
            println!("S");
        } else if action == -1 {
            println!("L");
        } else {
            println!("R");
        }
    }
}

impl std::cmp::PartialEq for State {
    fn eq(&self, other: &Self) -> bool {
        self.evaluated_score == other.evaluated_score
    }
}

impl std::cmp::PartialOrd for State {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        if self.evaluated_score == other.evaluated_score {
            Some(std::cmp::Ordering::Equal)
        } else if self.evaluated_score > other.evaluated_score {
            Some(std::cmp::Ordering::Greater)
        } else if self.evaluated_score < other.evaluated_score {
            Some(std::cmp::Ordering::Less)
        } else {
            None
        }
    }
}

impl std::cmp::Ord for State {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        if self.evaluated_score == other.evaluated_score {
            std::cmp::Ordering::Equal
        } else if self.evaluated_score > other.evaluated_score {
            std::cmp::Ordering::Greater
        } else {
            std::cmp::Ordering::Less
        }
    }
}

fn beam_search_action(
    state: &State,
    field: &Field,
    beam_width: usize,
    time_threshold: f64,
) -> isize {
    let mut now_beam = BinaryHeap::new();
    let mut best_state = state;
    now_beam.push(state.clone());
    let time_keeper = TimeKeeper::new(time_threshold);
    let mut turn = 0;

    for t in 0.. {
        let mut next_beam = BinaryHeap::new();
        turn = t + 1;

        for _ in 0..beam_width {
            if now_beam.is_empty() {
                break;
            }
            let now_state = now_beam.pop().unwrap();
            let actions = vec![0, -1, 1];
            for action in actions {
                let mut next_state = now_state.clone();
                if t == 0 {
                    next_state.first_action = action;
                    next_state.damage = 0;
                }
                next_state.advance(action);
                next_state.update_enemy(field);
                next_state.evaluate_score();
                if !next_state.is_dead {
                    next_beam.push(next_state);
                }
            }

            if time_keeper.isTimeOver() {
                break;
            }
        }
        now_beam = next_beam;
        best_state = now_beam.peek().unwrap();
        if best_state.is_done() || time_keeper.isTimeOver() {
            break;
        }
    }
    #[cfg(feature = "local")]
    {
        eprintln!("{}", turn);
    }
    best_state.first_action
}

#[derive(Default)]
struct Solver {}
impl Solver {
    fn solve(&mut self) {
        let mut state = State::new();
        let mut field = Field::new();

        #[cfg(feature = "local")]
        {
            eprintln!("Local Mode");
            let _: Vec<usize> = read_vec();
        }

        let start = std::time::Instant::now();
        let time_threshold = 1.0 * 1e-3; // [sec]

        while !state.is_done() {
            let N: isize = read();
            if N == -1 {
                return;
            }
            field.update(N as usize, state.turn);
            state.update_enemy(&field);
            let action = beam_search_action(&state, &field, 10000, time_threshold);
            state.advance(action);
            state.output(action);
        }
        eprintln!("Score: {}", state.score);
        eprintln!("S: {}", state.S);

        #[allow(unused_mut, unused_assignments)]
        let mut elapsed_time = start.elapsed().as_micros() as f64 * 1e-6;
        #[cfg(feature = "local")]
        {
            eprintln!("Local Mode");
            elapsed_time *= 1.5;
        }
        eprintln!("Elapsed time: {}sec", elapsed_time);
    }
}

#[macro_export]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $( $y: expr ),+) => {
        std::cmp::max($x, max!($( $y ),+))
    }
}
#[macro_export]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $( $y: expr ),+) => {
        std::cmp::min($x, min!($( $y ),+))
    }
}

fn main() {
    std::thread::Builder::new()
        .stack_size(128 * 1024 * 1024)
        .spawn(|| Solver::default().solve())
        .unwrap()
        .join()
        .unwrap();
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
0