Skip to content

Commit 3513bd4

Browse files
Add a sample code for H - Two SAT
1 parent ee2340d commit 3513bd4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

examples/practice2_h_two_sat.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use ac_library_rs::TwoSat;
2+
use std::io::Read;
3+
4+
fn main() {
5+
let mut buf = String::new();
6+
std::io::stdin().read_to_string(&mut buf).unwrap();
7+
let mut input = buf.split_whitespace();
8+
9+
let n = input.next().unwrap().parse().unwrap();
10+
let d = input.next().unwrap().parse().unwrap();
11+
let xs = (0..2 * n)
12+
.map(|_| input.next().unwrap().parse().unwrap())
13+
.collect::<Vec<i32>>();
14+
15+
let mut sat = TwoSat::new(2*n);
16+
for i in 0..2 * n {
17+
sat.add_clause(i, i % 2 == 0, i ^ 1, i % 2 == 0);
18+
}
19+
for (i, x) in xs.iter().enumerate() {
20+
for (j, y) in xs[..i].iter().enumerate() {
21+
if (x - y).abs() < d {
22+
sat.add_clause(i, false, j, false);
23+
}
24+
}
25+
}
26+
if sat.satisfiable() {
27+
println!("Yes");
28+
let ans = sat.answer();
29+
for i in 0..n {
30+
println!("{}", xs[2 * i + ans[2 * i + 1] as usize]);
31+
}
32+
} else {
33+
println!("No");
34+
}
35+
}

0 commit comments

Comments
 (0)