use std::io; use std::time::Instant; struct Timer { start_time: Instant, } impl Timer { fn new() -> Timer { Timer { start_time: Instant::now(), } } fn now_ms(&self) -> u128 { (Instant::now() - self.start_time).as_millis() } } #[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], } pub struct Problem { cards: Vec, operation_count: usize, } 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 = 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::>(); Problem { cards, operation_count: 50, } } mod greedy { use crate::{Card, Problem}; #[derive(Clone)] struct State { card_pool: Vec, } const TARGET_VALUE: u64 = 50_0000_0000_0000_0000; impl State { fn new(problem: &Problem) -> State { State { card_pool: problem.cards.clone(), } } fn copy_from(&mut self, state: &State) { for i in 0..state.card_pool.len() { self.card_pool[i] = state.card_pool[i] } } fn manipulate(&mut self, 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.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; } } 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(c1, c2); } // calculate score // ⌊2000000−100000 log 10 (max(V_1 ,V_2)+1)⌋ 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 } pub fn solve(problem: &Problem) -> Vec<(u8, u8)> { let mut state = State::new(problem); let mut init_state = State::new(problem); let mut ret = vec![]; for turn in 1..=problem.operation_count { // 平均化して改善率の最も高い組合せを選択 let mut best_improve_pair = (0, 0); let mut best_score = std::u64::MIN; let c1 = 0; for c2 in c1 + 1..state.card_pool.len() { init_state.copy_from(&state); init_state.manipulate(c1, c2); let score = simulate_with_greedy(problem, &mut init_state, problem.operation_count - turn); if best_score < score { best_score = score; best_improve_pair = (c1, c2); } } let (c1, c2) = best_improve_pair; state.manipulate(c1, c2); ret.push((c1 as u8, c2 as u8)); } ret } } #[derive(Clone)] struct State { operation_list: Vec<(u8, u8)>, } const TARGET_VALUE: u64 = 50_0000_0000_0000_0000; impl State { fn new(problem: &Problem) -> State { State { operation_list: vec![] } } } fn calculate_cost_diff(first: u64, second: u64) -> u64 { first.abs_diff(TARGET_VALUE).max(second.abs_diff(TARGET_VALUE)) } fn evaluate(problem: &Problem, state: &State) -> i64 { let mut card_pool = problem.cards.clone(); for (c1, c2) in state.operation_list.iter() { // 平均化して改善率の最も高い組合せを選択 let c1 = *c1 as usize; let c2 = *c2 as usize; // manipulate let first = (card_pool[c1].number[0] + card_pool[c2].number[0]) / 2; let second = (card_pool[c1].number[1] + card_pool[c2].number[1]) / 2; card_pool[c1].number = [first, second]; card_pool[c2].number = [first, second]; } let diff = calculate_cost_diff(card_pool[0].number[0], card_pool[0].number[1]); (200_0000f64 - 10_0000f64 * (diff as f64).log10()).floor() as i64 } fn solve(problem: &Problem) -> Vec<(usize, usize)> { let timer = Timer::new(); let mut state = State::new(problem); let init_operations = greedy::solve(problem); state.operation_list = init_operations; let mut eval = evaluate(problem, &state); let mut best_eval = eval; let mut best_state = state.clone(); let start_temp: f64 = 3e4; let end_temp: f64 = start_temp / 3e3; let mut progress = 0.0; let mut temp: f64 = start_temp.powf(1.0 - progress) * end_temp.powf(progress); let mut rng = Xorshift::new(); let mut fail_count = 0; for iter in 0.. { // 1点入れ替え let is_first = rng.rand(2) == 0; let oi = rng.rand(problem.operation_count as u64); let (prev_c1, prev_c2) = state.operation_list[oi as usize]; if is_first { let new_c1 = rng.rand(problem.cards.len() as u64 - 1) as u8; if new_c1 == prev_c1 || new_c1 == prev_c2 { continue; } state.operation_list[oi as usize] = (new_c1, prev_c2); } else { let new_c2 = rng.rand(problem.cards.len() as u64 - 1) as u8; if new_c2 == prev_c1 || new_c2 == prev_c2 { continue; } state.operation_list[oi as usize] = (prev_c1, new_c2); } let new_eval = evaluate(problem, &state); let accept = (-(new_eval - eval) as f64 / temp).exp() < rng.randf(); if accept { eval = new_eval; if best_eval < eval { best_eval = eval; best_state = state.clone(); eprintln!("best eval: {}", best_eval); fail_count = 0; } else { fail_count += 1; if fail_count == 2000 { state = best_state.clone(); eval = best_eval; } } } else { // rollback state.operation_list[oi as usize] = (prev_c1, prev_c2); fail_count += 1; if fail_count == 2000 { state = best_state.clone(); eval = best_eval; } } if iter % 128 == 0 { let elapsed_ms = timer.now_ms(); if elapsed_ms > 950 { eprintln!("# of iter: {}", iter); break; } progress = 0.0; temp = start_temp.powf(1.0 - progress) * end_temp.powf(progress); } } best_state .operation_list .into_iter() .map(|(c1, c2)| (c1 as usize + 1, c2 as usize + 1)) .collect::>() } fn main() { let problem = read_problem(); let ret = solve(&problem); println!("{}", ret.len()); for (c1, c2) in ret.iter() { println!("{} {}", c1, c2); } }