Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample code for the practice contest #52

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
24 changes: 24 additions & 0 deletions examples/practice2_a_disjoint_set_union.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_a
use ac_library_rs::Dsu;
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;

fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let n = input.next().unwrap().parse().unwrap();
let mut dsu = Dsu::new(n);
for _ in 0..input.next().unwrap().parse().unwrap() {
let t = input.next().unwrap().parse().unwrap();
let u = input.next().unwrap().parse().unwrap();
let v = input.next().unwrap().parse().unwrap();
match t {
0 => {
dsu.merge(u, v);
}
1 => println!("{}", dsu.same(u, v) as i32),
_ => {}
}
}
}
32 changes: 32 additions & 0 deletions examples/practice2_b_fenwick_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_b
use ac_library_rs::FenwickTree;
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;

fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let n = input.next().unwrap().parse().unwrap();
let q = input.next().unwrap().parse().unwrap();
let mut tree = FenwickTree::<u64>::new(n, 0);
for i in 0..n {
let a: u64 = input.next().unwrap().parse().unwrap();
tree.add(i, a);
}
for _ in 0..q {
match input.next().unwrap().parse().unwrap() {
0 => {
let p = input.next().unwrap().parse().unwrap();
let x: u64 = input.next().unwrap().parse().unwrap();
tree.add(p, x);
}
1 => {
let l = input.next().unwrap().parse().unwrap();
let r = input.next().unwrap().parse().unwrap();
println!("{}", tree.sum(l, r));
}
_ => {}
}
}
}
17 changes: 17 additions & 0 deletions examples/practice2_c_floor_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_c
use ac_library_rs::floor_sum;
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;

fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

for _ in 0..input.next().unwrap().parse().unwrap() {
let n = input.next().unwrap().parse().unwrap();
let m = input.next().unwrap().parse().unwrap();
let a = input.next().unwrap().parse().unwrap();
let b = input.next().unwrap().parse().unwrap();
println!("{}", floor_sum(n, m, a, b));
}
}
44 changes: 44 additions & 0 deletions examples/practice2_e_mincostflow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_e
use ac_library_rs::MinCostFlowGraph;
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;

const MAX: i64 = 1_000_000_000;

#[allow(clippy::needless_range_loop)]
fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let n = input.next().unwrap().parse().unwrap();
let k = input.next().unwrap().parse().unwrap();
let a: Vec<Vec<i64>> = (0..n)
.map(|_| input.by_ref().take(n).map(|s| s.parse().unwrap()).collect())
.collect();

let mut graph = MinCostFlowGraph::new(102);
for i in 0..n {
for j in 0..n {
graph.add_edge(i, 50 + j, 1, MAX - a[i][j]);
}
}
for i in 0..n {
graph.add_edge(100, i, k, 0);
graph.add_edge(50 + i, 101, k, 0);
}
graph.add_edge(100, 101, n as i64 * k, MAX);

let (max_flow, min_cost) = graph.flow(100, 101, n as i64 * k);
println!("{}", max_flow * MAX - min_cost);

(0..n)
.map(|i| {
(0..n)
.map(|j| match graph.get_edge(i * n + j).flow {
1 => 'X',
_ => '.',
})
.collect()
})
.for_each(|s: String| println!("{}", s));
}
39 changes: 39 additions & 0 deletions examples/practice2_f_convolution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_f
use ac_library_rs::{convolution, modint::ModInt998244353 as Mint};
use std::io::prelude::*;

pub fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let n: usize = input.next().unwrap().parse().unwrap();
let m: usize = input.next().unwrap().parse().unwrap();
let a: Vec<Mint> = input
.by_ref()
.take(n)
.map(str::parse)
.map(Result::unwrap)
.collect();
let b: Vec<Mint> = input
.by_ref()
.take(m)
.map(str::parse)
.map(Result::unwrap)
.collect();

print_oneline(convolution::convolution(&a, &b));
}

fn print_oneline<I: IntoIterator<Item = T>, T: std::fmt::Display>(values: I) {
let out = std::io::stdout();
let mut out = std::io::BufWriter::new(out.lock());
for (i, v) in values.into_iter().enumerate() {
if i == 0 {
write!(&mut out, "{}", v).unwrap();
} else {
write!(&mut out, " {}", v).unwrap();
}
}
writeln!(&mut out).unwrap();
}
27 changes: 27 additions & 0 deletions examples/practice2_g_scc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_g
use ac_library_rs::SccGraph;
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;

fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let n = input.next().unwrap().parse().unwrap();
let m = input.next().unwrap().parse().unwrap();
let mut graph = SccGraph::new(n);
for _ in 0..m {
let a = input.next().unwrap().parse().unwrap();
let b = input.next().unwrap().parse().unwrap();
graph.add_edge(a, b);
}
let scc = graph.scc();
println!("{}", scc.len());
for cc in scc {
print!("{}", cc.len());
for v in cc {
print!(" {}", v);
}
println!();
}
}
36 changes: 36 additions & 0 deletions examples/practice2_h_two_sat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_h
use ac_library_rs::TwoSat;
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;

fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let n = input.next().unwrap().parse().unwrap();
let d = input.next().unwrap().parse().unwrap();
let xs = (0..2 * n)
.map(|_| input.next().unwrap().parse().unwrap())
.collect::<Vec<i32>>();

let mut sat = TwoSat::new(2 * n);
for i in 0..2 * n {
sat.add_clause(i, i % 2 == 0, i ^ 1, i % 2 == 0);
}
for (i, x) in xs.iter().enumerate() {
for (j, y) in xs[..i].iter().enumerate() {
if (x - y).abs() < d {
sat.add_clause(i, false, j, false);
}
}
}
if sat.satisfiable() {
println!("Yes");
let ans = sat.answer();
for i in 0..n {
println!("{}", xs[2 * i + ans[2 * i + 1] as usize]);
}
} else {
println!("No");
}
}
17 changes: 17 additions & 0 deletions examples/practice2_i_number_of_substrings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_i
use ac_library_rs::{lcp_array, suffix_array};
TonalidadeHidrica marked this conversation as resolved.
Show resolved Hide resolved
use std::io::Read;
use std::iter;

fn main() {
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
let s = s.trim();
let suffix_array = suffix_array(s);
let ans: u64 = iter::once(0)
.chain(lcp_array(s, &suffix_array))
.zip(suffix_array)
.map(|(c, i)| (s.len() - i - c) as u64)
.sum();
println!("{}", ans);
}