結果

問題 No.5020 Averaging
ユーザー akidaiakidai
提出日時 2024-02-25 16:03:22
言語 Rust
(1.77.0)
結果
AC  
実行時間 996 ms / 1,000 ms
コード長 5,047 bytes
コンパイル時間 1,952 ms
コンパイル使用メモリ 189,924 KB
実行使用メモリ 196,480 KB
スコア 23,472,294
最終ジャッジ日時 2024-02-25 16:04:16
合計ジャッジ時間 53,709 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 995 ms
190,336 KB
testcase_01 AC 993 ms
191,744 KB
testcase_02 AC 994 ms
195,456 KB
testcase_03 AC 995 ms
190,336 KB
testcase_04 AC 995 ms
191,744 KB
testcase_05 AC 994 ms
184,832 KB
testcase_06 AC 993 ms
187,520 KB
testcase_07 AC 994 ms
194,304 KB
testcase_08 AC 995 ms
191,616 KB
testcase_09 AC 995 ms
191,616 KB
testcase_10 AC 994 ms
192,512 KB
testcase_11 AC 993 ms
185,984 KB
testcase_12 AC 993 ms
187,520 KB
testcase_13 AC 996 ms
191,872 KB
testcase_14 AC 994 ms
173,824 KB
testcase_15 AC 995 ms
189,312 KB
testcase_16 AC 994 ms
189,184 KB
testcase_17 AC 994 ms
191,488 KB
testcase_18 AC 994 ms
194,560 KB
testcase_19 AC 995 ms
192,128 KB
testcase_20 AC 996 ms
190,080 KB
testcase_21 AC 994 ms
191,104 KB
testcase_22 AC 994 ms
191,744 KB
testcase_23 AC 993 ms
189,952 KB
testcase_24 AC 994 ms
191,744 KB
testcase_25 AC 995 ms
190,464 KB
testcase_26 AC 996 ms
191,616 KB
testcase_27 AC 994 ms
186,240 KB
testcase_28 AC 994 ms
186,112 KB
testcase_29 AC 995 ms
192,000 KB
testcase_30 AC 993 ms
189,184 KB
testcase_31 AC 995 ms
191,616 KB
testcase_32 AC 995 ms
190,976 KB
testcase_33 AC 993 ms
191,488 KB
testcase_34 AC 994 ms
191,744 KB
testcase_35 AC 995 ms
191,744 KB
testcase_36 AC 995 ms
187,904 KB
testcase_37 AC 994 ms
189,696 KB
testcase_38 AC 995 ms
192,768 KB
testcase_39 AC 995 ms
191,616 KB
testcase_40 AC 995 ms
183,680 KB
testcase_41 AC 992 ms
186,880 KB
testcase_42 AC 996 ms
191,744 KB
testcase_43 AC 993 ms
189,312 KB
testcase_44 AC 994 ms
191,616 KB
testcase_45 AC 994 ms
191,616 KB
testcase_46 AC 995 ms
193,664 KB
testcase_47 AC 995 ms
191,744 KB
testcase_48 AC 995 ms
196,480 KB
testcase_49 AC 993 ms
187,520 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: variable does not need to be mutable
  --> Main.rs:44:13
   |
44 |         let mut max_score = 2000000;
   |             ----^^^^^^^^^
   |             |
   |             help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

warning: unused variable: `target`
  --> Main.rs:90:13
   |
90 |         let target = 10_i64.pow(17) * 5;
   |             ^^^^^^ help: if this is intentional, prefix it with an underscore: `_target`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `time`
  --> Main.rs:91:13
   |
91 |         let time = Instant::now();
   |             ^^^^ help: if this is intentional, prefix it with an underscore: `_time`

warning: variable `itr` is assigned to, but never used
  --> Main.rs:92:17
   |
92 |         let mut itr = 0;
   |                 ^^^
   |
   = note: consider using `_itr` instead

warning: unused variable: `cards`
   --> Main.rs:104:21
    |
104 |                 let cards = &state.cards;
    |                     ^^^^^ help: if this is intentional, prefix it with an underscore: `_cards`

warning: 5 warnings emitted

ソースコード

diff #

use std::cmp::{max, min};
use std::io;
use std::collections::BinaryHeap;
use std::time::Instant;


#[derive(Debug, Clone)]
struct Node {
    cards: Vec<(i64, i64)>,
    score: i64,
    history: Vec<(usize, usize)>,
}
impl Eq for Node {}
impl PartialEq for Node {
    fn eq(&self, other: &Self) -> bool {
        self.score == other.score
    }
}
impl Ord for Node {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.score.cmp(&other.score)
    }
}
impl PartialOrd for Node {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Node {
    fn new(cards: Vec<(i64, i64)>, history: Vec<(usize, usize)>, calced_score: i64) -> Self {
        let mut score = calced_score;
        if score <= -1 {
            score = Self::calc_score(&cards);
        }
        Self {
            cards,
            score,
            history,
        }
    }

    fn calc_score(cards: &Vec<(i64, i64)>) -> i64 {
        let mut max_score = 2000000;
        let target = 10_i64.pow(17) * 5;
        let mut result_score = 0;
        for i in 1..cards.len() {
            let new_card = ((cards[i].0 + cards[0].0) / 2, (cards[i].1 + cards[0].1) / 2);
            result_score = max(result_score, max_score - (100000.0 * (max((new_card.0 - target).abs(), (new_card.1 - target).abs()) as f64 + 1.0).log10()) as i64);
        }
        result_score
    }

    fn build_next(&self, turn: (usize, usize)) -> Self {
        let mut cards = self.cards.clone();
        let new_card = ((cards[turn.0].0 + cards[turn.1].0) / 2, (cards[turn.0].1 + cards[turn.1].1) / 2);
        cards[turn.0] = new_card;
        cards[turn.1] = new_card;
        let mut history = self.history.clone();
        history.push(turn);
        if turn.0 == 0 {
            Self::new(cards, history, -1)
        } else {
            let new_card_0 = ((cards[0].0 + new_card.0) / 2, (cards[0].1 + new_card.1) / 2);
            let mut score = self.score;
            score = max(score, 2000000 - (100000.0 * (max((new_card_0.0 - 500000000000000000).abs(), (new_card_0.1 - 500000000000000000).abs()) as f64 + 1.0).log10()) as i64);

            Self::new(cards, history, score)
        }
    }
}

struct Solver {
    n: usize,
    states: Vec<BinaryHeap<Node>>,
    time: Instant,
}

impl Solver {
    fn new(n: usize, cards: Vec<(i64, i64)>) -> Self {
        let mut states = vec![BinaryHeap::new(); 51];
        let node = Node::new(cards, vec![], -1);
        states[0].push(node);
        let time = Instant::now();
        Self { n, states, time }
    }

    fn solve(&mut self) {
        let n = self.n;
        let target = 10_i64.pow(17) * 5;
        let time = Instant::now();
        let mut itr = 0;
        while self.time.elapsed().as_millis() < 950 {
            itr += 1;
            for i in 0..50 {
                if self.time.elapsed().as_millis() >= 950 {
                    break;
                }
                let state = &self.states[i].pop();
                if state.is_none() {
                    continue;
                }
                let state = state.as_ref().unwrap();
                let cards = &state.cards;
                // eprintln!("{}: {}", itr, i + 1);
                let mut best_score = BinaryHeap::new();
                for p in 1..n {
                    self.states[i + 1].push(state.build_next((0, p)));
                    
                    if self.time.elapsed().as_millis() >= 950 {
                        break;
                    }
                }
                for p in 1..n {
                    for q in p + 1..n {
                        let next_state = state.build_next((p, q));
                        best_score.push(next_state);
                        
                        if self.time.elapsed().as_millis() >= 950 {
                            break;
                        }
                    }
                }
                for _ in 0..min(10, best_score.len()) {
                    self.states[i + 1].push(best_score.pop().unwrap());
                }
            }
        }
    }

    fn print(&self) {
        let ans_state = self.states[50].peek().unwrap();
        let ans = &ans_state.history;
        println!("{}", ans.len());
        for a in ans {
            println!("{} {}", a.0 + 1, a.1 + 1);
        }
        
        for i in 0..self.n {
            eprintln!("{}: {:?}", i + 1, ans_state.cards[i]);
        }
    }
}

fn main() {
    let mut buffer = String::new();
    io::stdin().read_line(&mut buffer).unwrap();
    let n: usize = buffer.trim().parse().unwrap();
    let mut cards = vec![];
    for _ in 0..n {
        let mut buffer = String::new();
        io::stdin().read_line(&mut buffer).unwrap();
        let mut iter = buffer.trim().split_whitespace();
        let x: i64 = iter.next().unwrap().parse().unwrap();
        let y: i64 = iter.next().unwrap().parse().unwrap();
        cards.push((x, y));
    }
    let mut solver = Solver::new(n, cards);
    solver.solve();
    solver.print();
}
0