結果

問題 No.5017 Tool-assisted Shooting
ユーザー 👑 terry_u16terry_u16
提出日時 2023-07-16 16:04:50
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,173 ms / 2,000 ms
コード長 7,458 bytes
コンパイル時間 2,339 ms
コンパイル使用メモリ 156,260 KB
実行使用メモリ 24,408 KB
スコア 4,003,362
平均クエリ数 990.00
最終ジャッジ日時 2023-07-16 16:06:51
合計ジャッジ時間 118,506 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,079 ms
23,652 KB
testcase_01 AC 1,146 ms
23,412 KB
testcase_02 AC 995 ms
23,412 KB
testcase_03 AC 1,099 ms
24,060 KB
testcase_04 AC 1,048 ms
24,348 KB
testcase_05 AC 1,033 ms
23,412 KB
testcase_06 AC 1,115 ms
24,024 KB
testcase_07 AC 1,108 ms
23,376 KB
testcase_08 AC 1,037 ms
23,436 KB
testcase_09 AC 1,078 ms
24,276 KB
testcase_10 AC 1,087 ms
23,412 KB
testcase_11 AC 1,049 ms
24,276 KB
testcase_12 AC 1,047 ms
23,388 KB
testcase_13 AC 1,124 ms
23,640 KB
testcase_14 AC 1,114 ms
23,424 KB
testcase_15 AC 1,065 ms
24,048 KB
testcase_16 AC 1,066 ms
23,640 KB
testcase_17 AC 1,090 ms
24,408 KB
testcase_18 AC 1,090 ms
24,036 KB
testcase_19 AC 1,095 ms
23,412 KB
testcase_20 AC 1,100 ms
23,412 KB
testcase_21 AC 1,123 ms
24,372 KB
testcase_22 AC 1,042 ms
24,360 KB
testcase_23 AC 1,065 ms
23,556 KB
testcase_24 AC 1,142 ms
24,384 KB
testcase_25 AC 1,169 ms
23,844 KB
testcase_26 AC 1,079 ms
23,844 KB
testcase_27 AC 1,046 ms
24,060 KB
testcase_28 AC 1,131 ms
23,844 KB
testcase_29 AC 1,046 ms
23,688 KB
testcase_30 AC 1,036 ms
24,276 KB
testcase_31 AC 1,068 ms
24,024 KB
testcase_32 AC 1,141 ms
23,844 KB
testcase_33 AC 1,150 ms
24,048 KB
testcase_34 AC 1,090 ms
24,060 KB
testcase_35 AC 1,094 ms
23,388 KB
testcase_36 AC 1,048 ms
24,336 KB
testcase_37 AC 1,073 ms
23,628 KB
testcase_38 AC 1,101 ms
23,676 KB
testcase_39 AC 1,134 ms
24,288 KB
testcase_40 AC 1,155 ms
23,556 KB
testcase_41 AC 1,060 ms
23,556 KB
testcase_42 AC 1,132 ms
24,264 KB
testcase_43 AC 1,095 ms
23,628 KB
testcase_44 AC 1,111 ms
23,832 KB
testcase_45 AC 1,085 ms
24,372 KB
testcase_46 AC 1,162 ms
24,000 KB
testcase_47 AC 1,011 ms
23,376 KB
testcase_48 AC 1,131 ms
24,024 KB
testcase_49 AC 1,079 ms
23,436 KB
testcase_50 AC 1,113 ms
23,676 KB
testcase_51 AC 1,074 ms
23,556 KB
testcase_52 AC 1,108 ms
24,024 KB
testcase_53 AC 1,114 ms
24,264 KB
testcase_54 AC 1,135 ms
24,024 KB
testcase_55 AC 1,076 ms
23,424 KB
testcase_56 AC 1,121 ms
23,652 KB
testcase_57 AC 1,047 ms
24,060 KB
testcase_58 AC 1,085 ms
23,388 KB
testcase_59 AC 1,132 ms
24,336 KB
testcase_60 AC 1,107 ms
23,544 KB
testcase_61 AC 1,065 ms
23,640 KB
testcase_62 AC 1,070 ms
24,060 KB
testcase_63 AC 1,094 ms
23,844 KB
testcase_64 AC 1,140 ms
24,372 KB
testcase_65 AC 1,173 ms
23,424 KB
testcase_66 AC 1,114 ms
23,556 KB
testcase_67 AC 1,080 ms
24,048 KB
testcase_68 AC 1,075 ms
24,036 KB
testcase_69 AC 1,110 ms
23,544 KB
testcase_70 AC 1,081 ms
24,012 KB
testcase_71 AC 1,096 ms
23,640 KB
testcase_72 AC 984 ms
23,544 KB
testcase_73 AC 1,119 ms
24,036 KB
testcase_74 AC 1,013 ms
24,348 KB
testcase_75 AC 1,114 ms
23,676 KB
testcase_76 AC 1,109 ms
24,360 KB
testcase_77 AC 1,143 ms
24,384 KB
testcase_78 AC 1,127 ms
24,360 KB
testcase_79 AC 1,070 ms
24,036 KB
testcase_80 AC 1,115 ms
23,412 KB
testcase_81 AC 1,103 ms
23,424 KB
testcase_82 AC 1,103 ms
23,640 KB
testcase_83 AC 1,073 ms
24,024 KB
testcase_84 AC 1,109 ms
23,544 KB
testcase_85 AC 1,136 ms
24,264 KB
testcase_86 AC 1,106 ms
23,556 KB
testcase_87 AC 1,070 ms
24,012 KB
testcase_88 AC 1,149 ms
23,844 KB
testcase_89 AC 1,109 ms
23,856 KB
testcase_90 AC 1,124 ms
23,640 KB
testcase_91 AC 1,164 ms
24,036 KB
testcase_92 AC 1,007 ms
23,664 KB
testcase_93 AC 1,101 ms
23,856 KB
testcase_94 AC 1,034 ms
24,024 KB
testcase_95 AC 1,095 ms
23,400 KB
testcase_96 AC 1,127 ms
23,544 KB
testcase_97 AC 1,117 ms
24,336 KB
testcase_98 AC 1,082 ms
23,376 KB
testcase_99 AC 1,082 ms
23,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::{collections::VecDeque, io::Write};

macro_rules! get {
      ($t:ty) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              line.trim().parse::<$t>().unwrap()
          }
      };
      ($($t:ty),*) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              let mut iter = line.split_whitespace();
              (
                  $(iter.next().unwrap().parse::<$t>().unwrap(),)*
              )
          }
      };
      ($t:ty; $n:expr) => {
          (0..$n).map(|_|
              get!($t)
          ).collect::<Vec<_>>()
      };
      ($($t:ty),*; $n:expr) => {
          (0..$n).map(|_|
              get!($($t),*)
          ).collect::<Vec<_>>()
      };
      ($t:ty ;;) => {
          {
              let mut line: String = String::new();
              std::io::stdin().read_line(&mut line).unwrap();
              line.split_whitespace()
                  .map(|t| t.parse::<$t>().unwrap())
                  .collect::<Vec<_>>()
          }
      };
      ($t:ty ;; $n:expr) => {
          (0..$n).map(|_| get!($t ;;)).collect::<Vec<_>>()
      };
}

pub trait ChangeMinMax {
    fn change_min(&mut self, v: Self) -> bool;
    fn change_max(&mut self, v: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn change_min(&mut self, v: T) -> bool {
        *self > v && {
            *self = v;
            true
        }
    }

    fn change_max(&mut self, v: T) -> bool {
        *self < v && {
            *self = v;
            true
        }
    }
}

const MAX_TURN: usize = 1000;
const DEFAULT_SIMULATION_LEN: usize = 15;
const HEIGHT: usize = 60;
const WIDTH: usize = 25;
const CENTER: usize = 12;
const L: usize = !0;
const C: usize = 0;
const R: usize = 1;

#[derive(Debug, Clone, Default)]
struct State {
    column: usize,
    power: u32,
    raw_score: u32,
    board: Vec<u64>,
    turn: usize,
    enemies: Vec<VecDeque<Enemy>>,
}

impl State {
    fn new() -> Self {
        Self {
            column: CENTER,
            power: 100,
            raw_score: 0,
            board: vec![0; WIDTH],
            turn: 0,
            enemies: vec![VecDeque::new(); WIDTH],
        }
    }

    fn level(&self) -> u32 {
        self.power / 100
    }

    fn move_enemy(&mut self) -> bool {
        for (i, b) in self.board.iter_mut().enumerate() {
            if *b & 1 > 0 {
                self.enemies[i].pop_front();
            }

            *b >>= 1;
        }

        !self.is_crash()
    }

    fn spawn(&mut self, enemies: &[(u32, u32, usize)]) {
        for &(hp, power, col) in enemies {
            self.board[col] |= 1 << (HEIGHT - 1);
            self.enemies[col].push_back(Enemy::new(hp, power));
        }
    }

    fn move_player(&mut self, direction: usize) -> bool {
        self.column = (self.column + direction + WIDTH) % WIDTH;
        !self.is_crash()
    }

    fn attack(&mut self) {
        let level = self.level();

        if let Some(enemy) = self.enemies[self.column].front_mut() {
            enemy.damage(level);

            if enemy.hp == 0 {
                self.raw_score += enemy.init_hp;
                self.power += enemy.power;
                self.enemies[self.column].pop_front();

                // ビットを下ろす
                let signed = self.board[self.column] as i64;
                let lsb = signed & -signed;
                self.board[self.column] ^= lsb as u64;
            }
        }
    }

    fn progress_turn(&mut self, enemies: &[(u32, u32, usize)], direction: usize) -> bool {
        let mut alive = true;
        alive &= self.move_enemy();
        self.spawn(enemies);
        alive &= self.move_player(direction);
        self.attack();
        self.turn += 1;

        alive
    }

    fn is_crash(&self) -> bool {
        (self.board[self.column] & 1) > 0
    }

    fn score(&self) -> f64 {
        let mut raw_score_point = self.raw_score as f64;
        let mut power_point = self.power as f64;

        for enemies in self.enemies.iter() {
            if let Some(enemy) = enemies.front() {
                let ratio = (enemy.init_hp - enemy.hp) as f64 / enemy.init_hp as f64;
                let ratio2 = ratio * ratio * 0.5;
                raw_score_point += enemy.init_hp as f64 * ratio2;
                power_point += enemy.power as f64 * ratio2;
            }
        }

        raw_score_point * self.turn as f64 + power_point * (MAX_TURN - self.turn) as f64
    }
}

#[derive(Debug, Clone, Copy, Default)]
struct Enemy {
    hp: u32,
    init_hp: u32,
    power: u32,
}

impl Enemy {
    fn new(hp: u32, power: u32) -> Self {
        Self {
            hp,
            init_hp: hp,
            power,
        }
    }

    fn damage(&mut self, attack: u32) {
        self.hp = self.hp.saturating_sub(attack);
    }
}

fn main() {
    let mut state = State::new();
    let mut turn = 0;

    while let Some(enemies) = read_spawns() {
        let mut current_states = vec![None; WIDTH];
        current_states[state.column] = Some((state.clone(), C));
        let simulation_len = DEFAULT_SIMULATION_LEN.min(MAX_TURN - turn);

        for iter in 0..simulation_len {
            let mut next_states: Vec<Option<(State, usize)>> = vec![None; WIDTH];

            for state in current_states.iter() {
                if let Some((state, first_dir)) = state {
                    for &dir in &[L, C, R] {
                        let mut state = state.clone();
                        let is_alive = state.progress_turn(&enemies, dir);

                        if !is_alive {
                            continue;
                        }

                        let next_col = state.column;

                        if next_states[next_col]
                            .as_ref()
                            .map_or(std::f64::MIN, |s| s.0.score())
                            < state.score()
                        {
                            let dir = if iter == 0 { dir } else { *first_dir };
                            next_states[next_col] = Some((state, dir));
                        }
                    }
                }
            }

            current_states = next_states;
        }

        let mut best_score = std::f64::MIN;
        let mut best_dir = C;

        for s in current_states.iter() {
            if let Some((state, dir)) = s {
                if best_score.change_max(state.score()) {
                    best_dir = *dir;
                }
            }
        }

        eprintln!("power: {}", state.power);
        eprintln!("score: {}", state.raw_score);
        eprintln!("turn: {}", turn + 1);
        eprintln!("best_score: {}", best_score);
        eprintln!("");

        write_direction(best_dir);
        state.progress_turn(&enemies, best_dir);
        turn += 1;

        if turn == MAX_TURN {
            break;
        }
    }
}

fn read_spawns() -> Option<Vec<(u32, u32, usize)>> {
    let n = get!(i32);

    if n < 0 {
        return None;
    }

    let mut enemies = vec![];

    for _ in 0..n {
        enemies.push(get!(u32, u32, usize));
    }

    Some(enemies)
}

fn write_direction(direction: usize) {
    match direction {
        L => println!("L"),
        C => println!("S"),
        R => println!("R"),
        _ => unreachable!(),
    }

    std::io::stdout().flush().unwrap();
}
0