結果

問題 No.5017 Tool-assisted Shooting
ユーザー terry_u16terry_u16
提出日時 2023-07-16 23:14:44
言語 Rust
(1.77.0 + proconio)
結果
TLE  
実行時間 -
コード長 11,010 bytes
コンパイル時間 1,590 ms
コンパイル使用メモリ 163,464 KB
実行使用メモリ 43,856 KB
スコア 4,551,848
平均クエリ数 950.00
最終ジャッジ日時 2023-07-16 23:17:53
合計ジャッジ時間 12,387 ms
ジャッジサーバーID
(参考情報)
judge9 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,711 ms
36,668 KB
testcase_01 AC 1,847 ms
43,856 KB
testcase_02 AC 1,739 ms
36,404 KB
testcase_03 AC 1,771 ms
37,600 KB
testcase_04 AC 1,780 ms
37,588 KB
testcase_05 AC 1,675 ms
39,720 KB
testcase_06 AC 1,745 ms
37,396 KB
testcase_07 AC 1,858 ms
37,080 KB
testcase_08 AC 1,779 ms
36,624 KB
testcase_09 AC 1,676 ms
37,532 KB
testcase_10 AC 1,767 ms
37,036 KB
testcase_11 AC 1,759 ms
38,716 KB
testcase_12 AC 1,694 ms
37,532 KB
testcase_13 AC 1,765 ms
36,796 KB
testcase_14 AC 1,911 ms
38,768 KB
testcase_15 AC 1,812 ms
37,888 KB
testcase_16 AC 1,742 ms
36,968 KB
testcase_17 AC 1,790 ms
38,216 KB
testcase_18 AC 1,760 ms
38,004 KB
testcase_19 AC 1,720 ms
38,488 KB
testcase_20 AC 1,742 ms
36,772 KB
testcase_21 AC 1,935 ms
37,412 KB
testcase_22 AC 1,783 ms
36,700 KB
testcase_23 AC 1,817 ms
37,408 KB
testcase_24 AC 1,798 ms
36,908 KB
testcase_25 AC 1,885 ms
36,664 KB
testcase_26 AC 1,899 ms
40,328 KB
testcase_27 AC 1,960 ms
36,812 KB
testcase_28 AC 1,917 ms
36,448 KB
testcase_29 AC 1,803 ms
37,084 KB
testcase_30 AC 1,930 ms
37,044 KB
testcase_31 AC 1,971 ms
37,356 KB
testcase_32 TLE -
testcase_33 AC 1,968 ms
36,740 KB
testcase_34 AC 1,774 ms
39,692 KB
testcase_35 AC 1,831 ms
37,960 KB
testcase_36 AC 1,726 ms
37,700 KB
testcase_37 AC 1,610 ms
37,172 KB
testcase_38 AC 1,682 ms
37,156 KB
testcase_39 AC 1,860 ms
37,444 KB
testcase_40 AC 1,884 ms
38,560 KB
testcase_41 AC 1,869 ms
37,724 KB
testcase_42 AC 1,803 ms
37,116 KB
testcase_43 AC 1,817 ms
37,992 KB
testcase_44 AC 1,934 ms
37,304 KB
testcase_45 AC 1,815 ms
37,324 KB
testcase_46 AC 1,840 ms
39,800 KB
testcase_47 AC 1,817 ms
36,828 KB
testcase_48 AC 1,842 ms
37,048 KB
testcase_49 AC 1,872 ms
37,364 KB
testcase_50 AC 1,938 ms
38,520 KB
testcase_51 AC 1,856 ms
36,088 KB
testcase_52 AC 1,902 ms
37,276 KB
testcase_53 AC 1,820 ms
37,528 KB
testcase_54 AC 1,968 ms
40,396 KB
testcase_55 AC 1,901 ms
38,176 KB
testcase_56 AC 1,967 ms
37,676 KB
testcase_57 AC 1,893 ms
37,632 KB
testcase_58 AC 1,860 ms
37,492 KB
testcase_59 TLE -
testcase_60 AC 1,928 ms
38,060 KB
testcase_61 AC 1,934 ms
37,996 KB
testcase_62 AC 1,822 ms
36,404 KB
testcase_63 AC 1,870 ms
38,756 KB
testcase_64 TLE -
testcase_65 AC 1,900 ms
38,496 KB
testcase_66 AC 1,850 ms
38,052 KB
testcase_67 TLE -
testcase_68 AC 1,796 ms
37,632 KB
testcase_69 AC 1,669 ms
40,072 KB
testcase_70 AC 1,668 ms
37,992 KB
testcase_71 AC 1,666 ms
37,832 KB
testcase_72 AC 1,707 ms
39,936 KB
testcase_73 AC 1,672 ms
37,976 KB
testcase_74 AC 1,586 ms
37,208 KB
testcase_75 AC 1,658 ms
37,728 KB
testcase_76 AC 1,829 ms
36,520 KB
testcase_77 AC 1,690 ms
37,188 KB
testcase_78 AC 1,864 ms
39,736 KB
testcase_79 AC 1,663 ms
39,736 KB
testcase_80 AC 1,601 ms
36,820 KB
testcase_81 AC 1,713 ms
37,124 KB
testcase_82 AC 1,741 ms
37,952 KB
testcase_83 AC 1,827 ms
36,884 KB
testcase_84 AC 1,699 ms
38,512 KB
testcase_85 AC 1,662 ms
37,860 KB
testcase_86 AC 1,706 ms
37,808 KB
testcase_87 AC 1,666 ms
37,364 KB
testcase_88 AC 1,705 ms
37,616 KB
testcase_89 AC 1,948 ms
38,748 KB
testcase_90 AC 1,770 ms
37,204 KB
testcase_91 AC 1,845 ms
38,328 KB
testcase_92 AC 1,825 ms
38,120 KB
testcase_93 AC 1,732 ms
37,528 KB
testcase_94 AC 1,732 ms
38,156 KB
testcase_95 AC 1,615 ms
40,084 KB
testcase_96 AC 1,669 ms
37,656 KB
testcase_97 AC 1,600 ms
37,340 KB
testcase_98 AC 1,504 ms
36,712 KB
testcase_99 AC 1,542 ms
38,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::{io::Write, time::Instant};

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 = 30;
const HEIGHT: usize = 60;
const WIDTH: usize = 25;
const CENTER: usize = 12;
const L: usize = !0;
const C: usize = 0;
const R: usize = 1;
const BEAM_WIDTH: usize = 15;
const TURN_STRIDE: usize = 2;

#[derive(Debug, Clone)]
struct State {
    column: usize,
    power: u32,
    raw_score: u32,
    score: f64,
    turn: usize,
    enemies: EnemyState,
}

impl State {
    fn new() -> Self {
        Self {
            column: CENTER,
            power: 100,
            raw_score: 0,
            turn: 0,
            score: 0.0,
            enemies: EnemyState::new(),
        }
    }

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

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

    fn attack(&mut self, enemy_collection: &EnemyCollection) {
        let level = self.level();

        if self.enemies.has_enemy(enemy_collection, self.column) {
            let (hp, power) = self.enemies.damage(enemy_collection, self.column, level);
            self.raw_score += hp;
            self.power += power;
        }
    }

    fn clean_up(&mut self, enemy_collection: &EnemyCollection) {
        self.enemies.clean_up_enemies(enemy_collection, self.turn);
    }

    fn progress_turn(&mut self, enemy_collection: &EnemyCollection, direction: usize) -> bool {
        let mut alive = true;
        alive &= !self.enemies.crash(enemy_collection, self.column, self.turn);
        self.move_player(direction);
        alive &= !self.enemies.crash(enemy_collection, self.column, self.turn);
        self.attack(enemy_collection);
        self.turn += 1;

        self.update_score(enemy_collection);

        alive
    }

    fn update_score(&mut self, enemy_collection: &EnemyCollection) {
        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.get(enemy_collection, col) {
                let ratio = self.enemies.damages[col] as f64 / enemy.hp as f64;

                if ratio == 0.0 {
                    continue;
                }

                let coef = coef * ratio * ratio * 0.8;
                raw_score_point += enemy.hp as f64 * coef;
                power_point += enemy.power as f64 * coef;
            }
        }

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

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

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

    fn is_out_of_range(&self, turn: usize) -> bool {
        self.spawn_turn + HEIGHT <= turn
    }

    fn is_bottom(&self, turn: usize) -> bool {
        self.spawn_turn + HEIGHT - 1 == turn
    }
}

#[derive(Debug, Clone)]
struct EnemyState {
    indices: [usize; WIDTH],
    damages: [u32; WIDTH],
}

impl EnemyState {
    fn new() -> Self {
        Self {
            indices: [0; WIDTH],
            damages: [0; WIDTH],
        }
    }

    fn has_enemy(&self, enemies: &EnemyCollection, column: usize) -> bool {
        self.get(enemies, column).is_some()
    }

    fn get<'a>(&self, enemies: &'a EnemyCollection, column: usize) -> Option<&'a Enemy> {
        enemies.get(column, self.indices[column])
    }

    fn crash(&self, enemies: &EnemyCollection, column: usize, turn: usize) -> bool {
        if let Some(enemy) = enemies.get(column, self.indices[column]) {
            enemy.is_bottom(turn)
        } else {
            false
        }
    }

    fn damage(&mut self, enemies: &EnemyCollection, column: usize, attack: u32) -> (u32, u32) {
        let enemy = enemies.get(column, self.indices[column]).unwrap();
        self.damages[column] += attack;

        if self.damages[column] >= enemy.hp {
            self.damages[column] = 0;
            self.indices[column] += 1;
            (enemy.hp, enemy.power)
        } else {
            (0, 0)
        }
    }

    fn clean_up_enemies(&mut self, enemies: &EnemyCollection, turn: usize) {
        let mut column = 0;
        let mut flag = enemies.clean_flags[turn];

        while flag > 0 {
            let tzcnt = flag.trailing_zeros();
            flag >>= tzcnt;
            column += tzcnt;

            let index = &mut self.indices[column as usize];
            let damage = &mut self.damages[column as usize];

            if let Some(enemy) = enemies.get(column as usize, *index) {
                if enemy.is_out_of_range(turn) {
                    *damage = 0;
                    *index += 1;
                }
            }

            flag >>= 1;
            column += 1;
        }
    }
}

#[derive(Debug, Clone)]
struct EnemyCollection {
    enemies: Vec<Vec<Enemy>>,
    clean_flags: Vec<u32>,
}

impl EnemyCollection {
    fn new() -> Self {
        Self {
            enemies: vec![vec![]; WIDTH],
            clean_flags: vec![0; MAX_TURN],
        }
    }

    fn spawn(&mut self, enemies: &[(u32, u32, usize)], turn: usize) {
        let mut flag = 0;

        for &(hp, power, col) in enemies {
            self.enemies[col].push(Enemy::new(hp, power, turn));
            flag |= 1 << col;
        }

        if turn + HEIGHT < MAX_TURN {
            self.clean_flags[turn + HEIGHT] = flag;
        }
    }

    fn get(&self, column: usize, index: usize) -> Option<&Enemy> {
        self.enemies[column].get(index)
    }
}

fn main() {
    let since = Instant::now();
    let mut state = State::new();
    let mut enemy_collection = EnemyCollection::new();
    let mut turn = 0;

    while let Some(enemies) = read_spawns() {
        enemy_collection.spawn(&enemies, turn);
        let mut all_states = vec![(state.clone(), [C; BEAM_WIDTH])];
        let mut current_states = vec![vec![]; WIDTH];
        current_states[state.column].push(0);
        let simulation_len = DEFAULT_SIMULATION_LEN.min(MAX_TURN - turn);

        for iter in 0..simulation_len {
            let mut next_states = vec![Vec::with_capacity(BEAM_WIDTH * 3); WIDTH];

            for &i in current_states.iter().flatten() {
                all_states[i].0.clean_up(&enemy_collection);

                for &dir in &[L, C, R] {
                    let (state, directions) = &all_states[i];
                    let mut state = state.clone();
                    let is_alive = state.progress_turn(&enemy_collection, dir);

                    if !is_alive {
                        continue;
                    }

                    let next_col = state.column;
                    let mut directions = directions.clone();

                    if iter < TURN_STRIDE {
                        directions[iter] = dir;
                    }

                    next_states[next_col].push(all_states.len());
                    all_states.push((state, directions));
                }
            }

            for next in next_states.iter_mut() {
                if next.len() > BEAM_WIDTH {
                    next.select_nth_unstable_by(BEAM_WIDTH, |&i, &j| {
                        all_states[j]
                            .0
                            .score
                            .partial_cmp(&all_states[i].0.score)
                            .unwrap()
                    });
                    next.truncate(BEAM_WIDTH);
                }
            }

            current_states = next_states;
        }

        let mut best_score = std::f64::MIN;
        let mut best_dir = [C; BEAM_WIDTH];

        for (state, dir) in current_states.iter().flatten().map(|&i| &all_states[i]) {
            if best_score.change_max(state.score) {
                best_dir = dir.clone();
            }
        }

        write_direction(best_dir[0]);
        state.clean_up(&enemy_collection);
        state.progress_turn(&enemy_collection, best_dir[0]);
        turn += 1;

        for i in 1..TURN_STRIDE {
            if let Some(enemies) = read_spawns() {
                enemy_collection.spawn(&enemies, turn);
                write_direction(best_dir[i]);
                state.clean_up(&enemy_collection);
                state.progress_turn(&enemy_collection, best_dir[i]);
                turn += 1;
            }
        }

        if turn == MAX_TURN {
            break;
        }
    }

    eprintln!("final score: {}", state.raw_score);
    eprintln!("{:.3}s", (Instant::now() - since).as_secs_f64());
}

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