結果
問題 | No.5020 Averaging |
ユーザー | xyz600600 |
提出日時 | 2024-02-25 15:09:36 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 631 ms / 1,000 ms |
コード長 | 8,545 bytes |
コンパイル時間 | 1,936 ms |
コンパイル使用メモリ | 209,940 KB |
実行使用メモリ | 6,548 KB |
スコア | 31,342,884 |
最終ジャッジ日時 | 2024-02-25 15:10:15 |
合計ジャッジ時間 | 36,399 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge12 |
純コード判定しない問題か言語 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 623 ms
6,548 KB |
testcase_01 | AC | 601 ms
6,548 KB |
testcase_02 | AC | 626 ms
6,548 KB |
testcase_03 | AC | 606 ms
6,548 KB |
testcase_04 | AC | 631 ms
6,548 KB |
testcase_05 | AC | 603 ms
6,548 KB |
testcase_06 | AC | 628 ms
6,548 KB |
testcase_07 | AC | 601 ms
6,548 KB |
testcase_08 | AC | 624 ms
6,548 KB |
testcase_09 | AC | 598 ms
6,548 KB |
testcase_10 | AC | 627 ms
6,548 KB |
testcase_11 | AC | 602 ms
6,548 KB |
testcase_12 | AC | 624 ms
6,548 KB |
testcase_13 | AC | 599 ms
6,548 KB |
testcase_14 | AC | 624 ms
6,548 KB |
testcase_15 | AC | 603 ms
6,548 KB |
testcase_16 | AC | 628 ms
6,548 KB |
testcase_17 | AC | 603 ms
6,548 KB |
testcase_18 | AC | 630 ms
6,548 KB |
testcase_19 | AC | 604 ms
6,548 KB |
testcase_20 | AC | 628 ms
6,548 KB |
testcase_21 | AC | 603 ms
6,548 KB |
testcase_22 | AC | 625 ms
6,548 KB |
testcase_23 | AC | 599 ms
6,548 KB |
testcase_24 | AC | 623 ms
6,548 KB |
testcase_25 | AC | 601 ms
6,548 KB |
testcase_26 | AC | 625 ms
6,548 KB |
testcase_27 | AC | 602 ms
6,548 KB |
testcase_28 | AC | 627 ms
6,548 KB |
testcase_29 | AC | 603 ms
6,548 KB |
testcase_30 | AC | 627 ms
6,548 KB |
testcase_31 | AC | 603 ms
6,548 KB |
testcase_32 | AC | 629 ms
6,548 KB |
testcase_33 | AC | 601 ms
6,548 KB |
testcase_34 | AC | 624 ms
6,548 KB |
testcase_35 | AC | 601 ms
6,548 KB |
testcase_36 | AC | 631 ms
6,548 KB |
testcase_37 | AC | 606 ms
6,548 KB |
testcase_38 | AC | 624 ms
6,548 KB |
testcase_39 | AC | 600 ms
6,548 KB |
testcase_40 | AC | 622 ms
6,548 KB |
testcase_41 | AC | 602 ms
6,548 KB |
testcase_42 | AC | 605 ms
6,548 KB |
testcase_43 | AC | 606 ms
6,548 KB |
testcase_44 | AC | 605 ms
6,548 KB |
testcase_45 | AC | 612 ms
6,548 KB |
testcase_46 | AC | 603 ms
6,548 KB |
testcase_47 | AC | 609 ms
6,548 KB |
testcase_48 | AC | 600 ms
6,548 KB |
testcase_49 | AC | 610 ms
6,548 KB |
ソースコード
use std::{collections::HashSet, io}; #[derive(Debug)] pub struct Xorshift { seed: u64, } impl Xorshift { pub fn new() -> Xorshift { Xorshift { seed: 0xf0fb588ca2196dac, } } pub fn with_seed(seed: u64) -> Xorshift { Xorshift { seed } } pub fn next(&mut self) -> u64 { self.seed = self.seed ^ (self.seed << 13); self.seed = self.seed ^ (self.seed >> 7); self.seed = self.seed ^ (self.seed << 17); self.seed } pub fn next_u128(&mut self) -> u128 { let first = self.next() as u128; let second = self.next() as u128; (first << 64) + second } pub fn rand(&mut self, m: u64) -> u64 { self.next() % m } // 0.0 ~ 1.0 pub fn randf(&mut self) -> f64 { use std::mem; const UPPER_MASK: u64 = 0x3FF0000000000000; const LOWER_MASK: u64 = 0xFFFFFFFFFFFFF; let tmp = UPPER_MASK | (self.next() & LOWER_MASK); let result: f64 = unsafe { mem::transmute(tmp) }; result - 1.0 } } #[derive(Clone, Copy)] struct Card { number: [u64; 2], } struct Problem { cards: Vec<Card>, operation_count: usize, hash_table: Vec<Vec<u128>>, } fn read_problem() -> Problem { // 標準入力からの読み取り用バッファ let mut input = String::new(); // N の読み取り io::stdin().read_line(&mut input).expect("Failed to read line"); let n: usize = input.trim().parse().expect("Input was not a number"); // 各 A_i, B_i の読み取り let mut pairs = Vec::new(); // (A_i, B_i) のペアを格納するベクター for _ in 0..n { input.clear(); // バッファをクリア io::stdin().read_line(&mut input).expect("Failed to read line"); // 空白で分割し、それぞれ u64 として解析 let parts: Vec<u64> = input .trim() .split_whitespace() .map(|x| x.parse().expect("Input was not a number")) .collect(); if parts.len() != 2 { eprintln!("Invalid input format"); continue; } // ベクターに追加 pairs.push((parts[0], parts[1])); } let cards = pairs .into_iter() .map(|(v1, v2)| Card { number: [v1, v2] }) .collect::<Vec<_>>(); let mut xorshift = Xorshift::new(); // table[card_id][select_count] let mut hash_table = vec![vec![0u128; 51]; n]; for y in 0..n { for x in 0..51 { hash_table[y][x] = xorshift.next_u128(); } } Problem { cards, operation_count: 50, hash_table, } } #[derive(Clone)] struct State { card_pool: Vec<Card>, select_count: Vec<u8>, hash: u128, operation_list: Vec<(u8, u8)>, } const TARGET_VALUE: u64 = 50_0000_0000_0000_0000; impl State { fn new(problem: &Problem) -> State { let mut hash = 0u128; for i in 0..problem.cards.len() { hash ^= problem.hash_table[i][0]; } State { card_pool: problem.cards.clone(), select_count: vec![0; problem.cards.len()], hash, operation_list: vec![], } } fn copy_from(&mut self, state: &State) { for i in 0..state.card_pool.len() { self.card_pool[i] = state.card_pool[i] } self.select_count.resize(state.select_count.len(), 0); self.select_count.copy_from_slice(&state.select_count); self.hash = state.hash; self.operation_list.resize(state.operation_list.len(), (0, 0)); self.operation_list.copy_from_slice(&state.operation_list); } fn manipulate(&mut self, problem: &Problem, c1: usize, c2: usize) { let first = (self.card_pool[c1].number[0] + self.card_pool[c2].number[0]) / 2; let second = (self.card_pool[c1].number[1] + self.card_pool[c2].number[1]) / 2; self.hash ^= problem.hash_table[c1][self.select_count[c1] as usize]; self.hash ^= problem.hash_table[c2][self.select_count[c2] as usize]; self.card_pool[c1].number[0] = first; self.card_pool[c2].number[0] = first; self.card_pool[c1].number[1] = second; self.card_pool[c2].number[1] = second; self.select_count[c1] += 1; self.select_count[c2] += 1; self.hash ^= problem.hash_table[c1][self.select_count[c1] as usize]; self.hash ^= problem.hash_table[c2][self.select_count[c2] as usize]; self.operation_list.push((c1 as u8, c2 as u8)); } } fn calculate_cost_diff(first: u64, second: u64) -> u64 { first.abs_diff(TARGET_VALUE).max(second.abs_diff(TARGET_VALUE)) } fn simulate_with_greedy(problem: &Problem, state: &mut State, turn: usize) -> u64 { let mut state = state.clone(); for _iter in 0..turn { // 平均化して改善率の最も高い組合せを選択 let mut best_improve_pair = (0, 0); let mut best_gain = std::i64::MIN; let c1 = 0; for c2 in c1 + 1..state.card_pool.len() { let first = (state.card_pool[c1].number[0] + state.card_pool[c2].number[0]) / 2; let second = (state.card_pool[c1].number[1] + state.card_pool[c2].number[1]) / 2; let before_c1_diff = calculate_cost_diff(state.card_pool[c1].number[0], state.card_pool[c1].number[1]); let before_c2_diff = calculate_cost_diff(state.card_pool[c2].number[0], state.card_pool[c2].number[1]); let before_diff = before_c1_diff + before_c2_diff; let after_diff = calculate_cost_diff(first, second) * 2; let gain = before_diff as i64 - after_diff as i64; if best_gain < gain { best_gain = gain; best_improve_pair = (c1, c2); } } let (c1, c2) = best_improve_pair; state.manipulate(problem, c1, c2); } // calculate score // ⌊2000000−100000 log 10 (max(V_1 ,V_2)+1)⌋ // higher is better let diff = calculate_cost_diff(state.card_pool[0].number[0], state.card_pool[0].number[1]); (200_0000f64 - 10_0000f64 * (diff as f64).log10()).floor() as u64 } fn solve(problem: &Problem) -> Vec<(usize, usize)> { let mut state_buffer1 = vec![State::new(problem)]; let mut state_buffer2 = vec![]; let mut init_state = state_buffer1[0].clone(); let beam_width = 50; let mut diff_list = vec![]; let mut hash_set = HashSet::<u128>::new(); let mut xorshift = Xorshift::new(); for turn in 1..=problem.operation_count { diff_list.clear(); let (before_buffer, mut after_buffer) = if turn % 2 == 1 { (state_buffer1, state_buffer2) } else { (state_buffer2, state_buffer1) }; for (i, s) in before_buffer.iter().enumerate() { let c1 = 0; for c2 in c1 + 1..s.card_pool.len() { init_state.copy_from(&s); init_state.manipulate(problem, c1, c2); let hash = init_state.hash; let score = simulate_with_greedy(problem, &mut init_state, problem.operation_count - turn); let noise = xorshift.next() % 100; let score = score * 100 + noise; diff_list.push((score as i64, hash, i, c1, c2)); } } diff_list.sort_by_key(|p| -p.0); hash_set.clear(); let mut ti = 0; for (_s, hash, si, c1, c2) in diff_list.iter() { if hash_set.contains(hash) { continue; } hash_set.insert(*hash); if after_buffer.len() <= ti { let state = State::new(problem); after_buffer.push(state); } after_buffer[ti].copy_from(&before_buffer[*si as usize]); after_buffer[ti].manipulate(problem, *c1, *c2); if after_buffer.len() >= beam_width { break; } ti += 1; } (state_buffer1, state_buffer2) = if turn % 2 == 1 { (before_buffer, after_buffer) } else { (after_buffer, before_buffer) }; } state_buffer1[0] .operation_list .iter() .map(|(c1, c2)| (1 + *c1 as usize, 1 + *c2 as usize)) .collect::<Vec<_>>() } fn main() { let problem = read_problem(); let ret = solve(&problem); println!("{}", ret.len()); for (c1, c2) in ret.iter() { println!("{} {}", c1, c2); } }