結果
問題 | No.5005 3-SAT |
ユーザー | terry_u16 |
提出日時 | 2022-04-29 16:12:38 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 1,903 ms / 2,000 ms |
コード長 | 9,619 bytes |
コンパイル時間 | 2,670 ms |
実行使用メモリ | 3,068 KB |
スコア | 65,309 |
最終ジャッジ日時 | 2022-04-29 16:15:58 |
合計ジャッジ時間 | 199,112 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge14 |
純コード判定しない問題か言語 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,902 ms
2,844 KB |
testcase_01 | AC | 1,902 ms
2,888 KB |
testcase_02 | AC | 1,902 ms
2,900 KB |
testcase_03 | AC | 1,901 ms
2,832 KB |
testcase_04 | AC | 1,902 ms
2,832 KB |
testcase_05 | AC | 1,902 ms
2,848 KB |
testcase_06 | AC | 1,902 ms
2,916 KB |
testcase_07 | AC | 1,902 ms
2,788 KB |
testcase_08 | AC | 1,902 ms
2,884 KB |
testcase_09 | AC | 1,902 ms
2,972 KB |
testcase_10 | AC | 1,901 ms
2,956 KB |
testcase_11 | AC | 1,901 ms
2,980 KB |
testcase_12 | AC | 1,903 ms
2,992 KB |
testcase_13 | AC | 1,902 ms
3,028 KB |
testcase_14 | AC | 1,901 ms
2,844 KB |
testcase_15 | AC | 1,902 ms
2,956 KB |
testcase_16 | AC | 1,901 ms
2,896 KB |
testcase_17 | AC | 1,901 ms
2,856 KB |
testcase_18 | AC | 1,902 ms
2,936 KB |
testcase_19 | AC | 1,902 ms
3,056 KB |
testcase_20 | AC | 1,902 ms
2,960 KB |
testcase_21 | AC | 1,902 ms
2,916 KB |
testcase_22 | AC | 1,901 ms
2,848 KB |
testcase_23 | AC | 1,901 ms
2,976 KB |
testcase_24 | AC | 1,902 ms
2,896 KB |
testcase_25 | AC | 1,902 ms
2,920 KB |
testcase_26 | AC | 1,902 ms
2,980 KB |
testcase_27 | AC | 1,902 ms
2,908 KB |
testcase_28 | AC | 1,902 ms
2,860 KB |
testcase_29 | AC | 1,902 ms
2,900 KB |
testcase_30 | AC | 1,902 ms
3,060 KB |
testcase_31 | AC | 1,902 ms
3,048 KB |
testcase_32 | AC | 1,902 ms
2,888 KB |
testcase_33 | AC | 1,902 ms
2,880 KB |
testcase_34 | AC | 1,902 ms
2,908 KB |
testcase_35 | AC | 1,901 ms
2,912 KB |
testcase_36 | AC | 1,901 ms
2,888 KB |
testcase_37 | AC | 1,902 ms
2,936 KB |
testcase_38 | AC | 1,902 ms
2,992 KB |
testcase_39 | AC | 1,902 ms
2,908 KB |
testcase_40 | AC | 1,902 ms
2,872 KB |
testcase_41 | AC | 1,901 ms
2,952 KB |
testcase_42 | AC | 1,901 ms
2,920 KB |
testcase_43 | AC | 1,902 ms
2,876 KB |
testcase_44 | AC | 1,902 ms
3,068 KB |
testcase_45 | AC | 1,902 ms
3,060 KB |
testcase_46 | AC | 1,902 ms
2,976 KB |
testcase_47 | AC | 1,902 ms
2,852 KB |
testcase_48 | AC | 1,901 ms
2,936 KB |
testcase_49 | AC | 1,902 ms
2,964 KB |
testcase_50 | AC | 1,902 ms
2,868 KB |
testcase_51 | AC | 1,902 ms
2,904 KB |
testcase_52 | AC | 1,902 ms
2,792 KB |
testcase_53 | AC | 1,902 ms
2,908 KB |
testcase_54 | AC | 1,901 ms
2,852 KB |
testcase_55 | AC | 1,901 ms
3,048 KB |
testcase_56 | AC | 1,902 ms
3,000 KB |
testcase_57 | AC | 1,902 ms
2,956 KB |
testcase_58 | AC | 1,902 ms
2,776 KB |
testcase_59 | AC | 1,902 ms
3,064 KB |
testcase_60 | AC | 1,901 ms
2,896 KB |
testcase_61 | AC | 1,902 ms
2,980 KB |
testcase_62 | AC | 1,901 ms
2,852 KB |
testcase_63 | AC | 1,902 ms
2,964 KB |
testcase_64 | AC | 1,902 ms
2,792 KB |
testcase_65 | AC | 1,902 ms
2,980 KB |
testcase_66 | AC | 1,901 ms
2,852 KB |
testcase_67 | AC | 1,901 ms
3,024 KB |
testcase_68 | AC | 1,902 ms
2,920 KB |
testcase_69 | AC | 1,902 ms
2,996 KB |
testcase_70 | AC | 1,902 ms
2,948 KB |
testcase_71 | AC | 1,902 ms
2,868 KB |
testcase_72 | AC | 1,901 ms
2,996 KB |
testcase_73 | AC | 1,901 ms
2,984 KB |
testcase_74 | AC | 1,902 ms
2,876 KB |
testcase_75 | AC | 1,902 ms
2,912 KB |
testcase_76 | AC | 1,902 ms
2,936 KB |
testcase_77 | AC | 1,902 ms
2,888 KB |
testcase_78 | AC | 1,902 ms
2,944 KB |
testcase_79 | AC | 1,901 ms
2,972 KB |
testcase_80 | AC | 1,902 ms
3,056 KB |
testcase_81 | AC | 1,902 ms
2,912 KB |
testcase_82 | AC | 1,902 ms
2,940 KB |
testcase_83 | AC | 1,902 ms
3,028 KB |
testcase_84 | AC | 1,902 ms
2,932 KB |
testcase_85 | AC | 1,901 ms
2,944 KB |
testcase_86 | AC | 1,902 ms
2,856 KB |
testcase_87 | AC | 1,902 ms
2,976 KB |
testcase_88 | AC | 1,902 ms
2,984 KB |
testcase_89 | AC | 1,902 ms
2,916 KB |
testcase_90 | AC | 1,902 ms
2,916 KB |
testcase_91 | AC | 1,901 ms
2,992 KB |
testcase_92 | AC | 1,901 ms
3,068 KB |
testcase_93 | AC | 1,902 ms
2,864 KB |
testcase_94 | AC | 1,902 ms
2,856 KB |
testcase_95 | AC | 1,902 ms
2,940 KB |
testcase_96 | AC | 1,902 ms
3,004 KB |
testcase_97 | AC | 1,901 ms
2,980 KB |
testcase_98 | AC | 1,901 ms
2,872 KB |
testcase_99 | AC | 1,902 ms
2,904 KB |
コンパイルメッセージ
warning: unused variable: `i` --> Main.rs:300:10 | 300 | for (i, (solution, choice)) in solution.iter().zip(input.choices.iter()).enumerate() { | ^ help: if this is intentional, prefix it with an underscore: `_i` | = note: `#[warn(unused_variables)]` on by default warning: constant is never used: `TIME_LIMIT` --> Main.rs:122:1 | 122 | const TIME_LIMIT: f64 = 1.95; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default warning: 2 warnings emitted
ソースコード
use std::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<_>>() }; } #[allow(unused_macros)] macro_rules! chmin { ($base:expr, $($cmps:expr),+ $(,)*) => {{ let cmp_min = min!($($cmps),+); if $base > cmp_min { $base = cmp_min; true } else { false } }}; } #[allow(unused_macros)] macro_rules! chmax { ($base:expr, $($cmps:expr),+ $(,)*) => {{ let cmp_max = max!($($cmps),+); if $base < cmp_max { $base = cmp_max; true } else { false } }}; } #[allow(unused_macros)] macro_rules! min { ($a:expr $(,)*) => {{ $a }}; ($a:expr, $b:expr $(,)*) => {{ std::cmp::min($a, $b) }}; ($a:expr, $($rest:expr),+ $(,)*) => {{ std::cmp::min($a, min!($($rest),+)) }}; } #[allow(unused_macros)] macro_rules! max { ($a:expr $(,)*) => {{ $a }}; ($a:expr, $b:expr $(,)*) => {{ std::cmp::max($a, $b) }}; ($a:expr, $($rest:expr),+ $(,)*) => {{ std::cmp::max($a, max!($($rest),+)) }}; } #[allow(unused_macros)] macro_rules! mat { ($e:expr; $d:expr) => { vec![$e; $d] }; ($e:expr; $d:expr $(; $ds:expr)+) => { vec![mat![$e $(; $ds)*]; $d] }; } #[derive(Debug, Clone, Copy)] struct Requirement { digit: usize, value: usize, } #[derive(Debug, Clone)] struct Choice { requirements: [Requirement; 3], } #[derive(Debug, Clone)] struct Input { since: Instant, choices: Vec<Choice>, } const CHOICES_COUNT: usize = 2048; const DIGIT_LEN: usize = 256; const TIME_LIMIT: f64 = 1.95; fn main() { let input = read_input(); let solution = solve(&input); write_output(&solution); } fn read_input() -> Input { let mut choices = vec![]; let mut counts = vec![0; DIGIT_LEN]; for _ in 0..CHOICES_COUNT { let (a, b, c, p, q, r) = get!(usize, usize, usize, usize, usize, usize); counts[a] += 1; counts[b] += 1; counts[c] += 1; let requirements = [ Requirement { digit: a, value: p }, Requirement { digit: b, value: q }, Requirement { digit: c, value: r }, ]; let choice = Choice { requirements }; choices.push(choice); } // TODO: 重なるケースとか真面目に見た方がいいかも for choice in choices.iter_mut() { choice .requirements .sort_unstable_by(|r0, r1| counts[r0.digit].cmp(&counts[r1.digit])); } let since = Instant::now(); let input = Input { since, choices }; input } fn solve(input: &Input) -> Vec<usize> { let init_solution = solve_rand(input); let solution = annealing(input, init_solution, 1.8); to_answer(input, &solution) } fn solve_rand(input: &Input) -> Vec<usize> { let mut best_solution = vec![0; CHOICES_COUNT]; let mut best_score = 0; let mut solution = vec![0; CHOICES_COUNT]; let mut counts = vec![0; DIGIT_LEN]; let mut rng = Xorshift::new(); let mut valid_choices = vec![]; 'main: while (Instant::now() - input.since).as_secs_f64() < 0.1 { solution.fill(0); counts.fill(0); for (i, choice) in input.choices.iter().enumerate() { valid_choices.clear(); for (j, req) in choice.requirements.iter().enumerate() { if counts[req.digit] == 0 || solution[req.digit] == req.value { valid_choices.push((j, req)); } } if valid_choices.len() == 0 { if chmax!(best_score, i) { best_solution = solution.clone(); } continue 'main; } let index = rng.rand(valid_choices.len()); let (j, choice) = valid_choices[index]; solution[i] = j; counts[choice.digit] += 1; } best_score = input.choices.len(); best_solution = solution.clone(); } eprintln!("score_init: {}", best_score); best_solution } fn annealing(input: &Input, initial_solution: Vec<usize>, duration: f64) -> Vec<usize> { let mut solution = initial_solution; let mut best_solution = solution.clone(); let mut current_score = calc_score(input, &solution); let mut best_score = current_score; let mut all_iter = 0; let mut valid_iter = 0; let mut accepted_count = 0; let mut update_count = 0; let mut rng = Xorshift::with_seed(42); let duration_inv = 1.0 / duration; let since = std::time::Instant::now(); let mut time = 0.0; let temp0 = 1e1; let temp1 = 1e-1; let mut inv_temp = 1.0 / temp0; while time < 1.0 { all_iter += 1; if (all_iter & ((1 << 4) - 1)) == 0 { time = (std::time::Instant::now() - since).as_secs_f64() * duration_inv; let temp = f64::powf(temp0, 1.0 - time) * f64::powf(temp1, time); inv_temp = 1.0 / temp; } // 変形 let index = rng.rand((best_score as usize + 100).min(CHOICES_COUNT)); let add = rng.rand(2) + 1; solution[index] = (solution[index] + add) % 3; // スコア計算 let new_score = calc_score(input, &solution); let score_diff = new_score - current_score; if score_diff >= 0 || rng.randf() < f64::exp(score_diff as f64 * inv_temp) { // 解の更新 current_score = new_score; accepted_count += 1; if chmax!(best_score, current_score) { best_solution = solution.clone(); update_count += 1; } } else { // ロールバック solution[index] = (solution[index] + 3 - add) % 3; } valid_iter += 1; } eprintln!("===== annealing ====="); eprintln!("score : {}", best_score); eprintln!("all iter : {}", all_iter); eprintln!("valid iter : {}", valid_iter); eprintln!("accepted : {}", accepted_count); eprintln!("updated : {}", update_count); eprintln!(""); eprintln!("score: {}", best_score); best_solution } fn calc_score(input: &Input, solution: &[usize]) -> i32 { let mut used = [false; DIGIT_LEN]; let mut values = [0; DIGIT_LEN]; for (i, (solution, choice)) in solution.iter().zip(input.choices.iter()).enumerate() { let req = choice.requirements[*solution]; if used[req.digit] && values[req.digit] != req.value { return i as i32; } used[req.digit] = true; values[req.digit] = req.value; } CHOICES_COUNT as i32 } fn to_answer(input: &Input, solution: &[usize]) -> Vec<usize> { let mut used = [false; DIGIT_LEN]; let mut values = vec![0; DIGIT_LEN]; for (i, (solution, choice)) in solution.iter().zip(input.choices.iter()).enumerate() { let req = choice.requirements[*solution]; if used[req.digit] && values[req.digit] != req.value { return values; } used[req.digit] = true; values[req.digit] = req.value; } values } fn write_output(solution: &[usize]) { let mut result = String::new(); for v in solution.iter().rev() { if *v == 0 { result.push('0'); } else { result.push('1'); } } println!("{}", result); } #[derive(Debug)] #[allow(dead_code)] pub struct Xorshift { seed: usize, } impl Xorshift { #[allow(dead_code)] pub fn new() -> Xorshift { Xorshift { seed: 0xf0fb588ca2196dac, } } #[allow(dead_code)] pub fn with_seed(seed: usize) -> Xorshift { Xorshift { seed: seed } } #[inline] #[allow(dead_code)] pub fn next(&mut self) -> usize { self.seed = self.seed ^ (self.seed << 13); self.seed = self.seed ^ (self.seed >> 7); self.seed = self.seed ^ (self.seed << 17); self.seed } #[inline] #[allow(dead_code)] pub fn rand(&mut self, m: usize) -> usize { self.next() % m } #[inline] #[allow(dead_code)] // 0.0 ~ 1.0 pub fn randf(&mut self) -> f64 { use std::mem; const UPPER_MASK: usize = 0x3FF0000000000000; const LOWER_MASK: usize = 0xFFFFFFFFFFFFF; let tmp = UPPER_MASK | (self.next() & LOWER_MASK); let result: f64 = unsafe { mem::transmute(tmp) }; result - 1.0 } }