結果

問題 No.5017 Tool-assisted Shooting
ユーザー terry_u16terry_u16
提出日時 2023-07-16 16:13:06
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 1,107 ms / 2,000 ms
コード長 7,718 bytes
コンパイル時間 1,580 ms
コンパイル使用メモリ 160,536 KB
実行使用メモリ 24,492 KB
スコア 4,099,105
平均クエリ数 1000.00
最終ジャッジ日時 2023-07-16 16:14:57
合計ジャッジ時間 109,342 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,034 ms
23,532 KB
testcase_01 AC 1,064 ms
24,060 KB
testcase_02 AC 900 ms
23,556 KB
testcase_03 AC 1,017 ms
23,376 KB
testcase_04 AC 946 ms
23,616 KB
testcase_05 AC 994 ms
23,544 KB
testcase_06 AC 1,006 ms
23,544 KB
testcase_07 AC 1,064 ms
23,844 KB
testcase_08 AC 950 ms
23,424 KB
testcase_09 AC 980 ms
24,012 KB
testcase_10 AC 1,007 ms
24,480 KB
testcase_11 AC 976 ms
24,336 KB
testcase_12 AC 985 ms
23,664 KB
testcase_13 AC 1,012 ms
23,652 KB
testcase_14 AC 1,057 ms
24,408 KB
testcase_15 AC 984 ms
23,640 KB
testcase_16 AC 1,022 ms
23,652 KB
testcase_17 AC 1,023 ms
23,976 KB
testcase_18 AC 1,021 ms
23,988 KB
testcase_19 AC 1,044 ms
23,424 KB
testcase_20 AC 1,016 ms
24,036 KB
testcase_21 AC 1,051 ms
23,388 KB
testcase_22 AC 972 ms
23,412 KB
testcase_23 AC 985 ms
24,036 KB
testcase_24 AC 1,070 ms
24,048 KB
testcase_25 AC 1,047 ms
23,664 KB
testcase_26 AC 988 ms
23,412 KB
testcase_27 AC 999 ms
23,388 KB
testcase_28 AC 1,070 ms
23,652 KB
testcase_29 AC 948 ms
23,544 KB
testcase_30 AC 982 ms
24,048 KB
testcase_31 AC 987 ms
24,264 KB
testcase_32 AC 1,053 ms
23,640 KB
testcase_33 AC 1,074 ms
24,036 KB
testcase_34 AC 982 ms
23,628 KB
testcase_35 AC 1,032 ms
23,400 KB
testcase_36 AC 959 ms
23,832 KB
testcase_37 AC 1,020 ms
24,360 KB
testcase_38 AC 1,000 ms
23,844 KB
testcase_39 AC 1,045 ms
23,640 KB
testcase_40 AC 1,082 ms
24,360 KB
testcase_41 AC 980 ms
24,360 KB
testcase_42 AC 1,030 ms
23,880 KB
testcase_43 AC 1,007 ms
24,264 KB
testcase_44 AC 1,052 ms
24,036 KB
testcase_45 AC 1,003 ms
24,264 KB
testcase_46 AC 1,069 ms
24,264 KB
testcase_47 AC 945 ms
23,400 KB
testcase_48 AC 1,037 ms
23,664 KB
testcase_49 AC 1,002 ms
23,676 KB
testcase_50 AC 1,008 ms
24,036 KB
testcase_51 AC 995 ms
23,436 KB
testcase_52 AC 1,029 ms
23,376 KB
testcase_53 AC 1,010 ms
24,024 KB
testcase_54 AC 1,026 ms
24,492 KB
testcase_55 AC 983 ms
23,544 KB
testcase_56 AC 1,018 ms
23,844 KB
testcase_57 AC 988 ms
23,628 KB
testcase_58 AC 1,016 ms
23,652 KB
testcase_59 AC 1,038 ms
23,376 KB
testcase_60 AC 988 ms
24,408 KB
testcase_61 AC 1,002 ms
23,256 KB
testcase_62 AC 963 ms
23,640 KB
testcase_63 AC 1,044 ms
23,652 KB
testcase_64 AC 1,042 ms
23,676 KB
testcase_65 AC 1,107 ms
23,388 KB
testcase_66 AC 1,040 ms
23,388 KB
testcase_67 AC 996 ms
24,384 KB
testcase_68 AC 986 ms
23,988 KB
testcase_69 AC 1,025 ms
23,640 KB
testcase_70 AC 991 ms
24,024 KB
testcase_71 AC 999 ms
24,336 KB
testcase_72 AC 967 ms
24,048 KB
testcase_73 AC 1,031 ms
24,336 KB
testcase_74 AC 931 ms
23,388 KB
testcase_75 AC 1,046 ms
24,036 KB
testcase_76 AC 999 ms
24,036 KB
testcase_77 AC 1,080 ms
23,412 KB
testcase_78 AC 1,019 ms
23,628 KB
testcase_79 AC 1,008 ms
23,544 KB
testcase_80 AC 1,035 ms
23,388 KB
testcase_81 AC 985 ms
24,048 KB
testcase_82 AC 1,041 ms
24,348 KB
testcase_83 AC 1,006 ms
23,400 KB
testcase_84 AC 1,016 ms
23,412 KB
testcase_85 AC 1,045 ms
24,048 KB
testcase_86 AC 1,015 ms
23,844 KB
testcase_87 AC 1,009 ms
24,336 KB
testcase_88 AC 1,073 ms
23,724 KB
testcase_89 AC 1,068 ms
23,664 KB
testcase_90 AC 1,049 ms
24,264 KB
testcase_91 AC 1,099 ms
23,544 KB
testcase_92 AC 930 ms
24,408 KB
testcase_93 AC 1,053 ms
23,640 KB
testcase_94 AC 980 ms
23,844 KB
testcase_95 AC 1,036 ms
24,336 KB
testcase_96 AC 1,044 ms
23,424 KB
testcase_97 AC 1,033 ms
23,856 KB
testcase_98 AC 1,008 ms
23,424 KB
testcase_99 AC 1,018 ms
24,012 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;
        let cols = [
            ((self.column + WIDTH - L) % WIDTH, 0.5),
            (self.column, 1.0),
            ((self.column + R) % WIDTH, 0.5),
        ];

        for &(col, coef) in &cols {
            if let Some(enemy) = self.enemies[col].front() {
                let ratio = (enemy.init_hp - enemy.hp) as f64 / enemy.init_hp as f64;
                let coef = coef * ratio * ratio * 0.5;
                raw_score_point += enemy.init_hp as f64 * coef;
                power_point += enemy.power as f64 * coef;
            }
        }

        let raw_score_coef = self.turn as f64;
        let power_point_coef = (MAX_TURN - self.turn) as f64;
        raw_score_point * raw_score_coef + power_point * power_point_coef
    }
}

#[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