diff --git a/atcoder/abc359/a.rs b/atcoder/abc359/a.rs new file mode 100644 index 00000000..749a8368 --- /dev/null +++ b/atcoder/abc359/a.rs @@ -0,0 +1,35 @@ +use std::io::Read; + +fn get_word() -> String { + let stdin = std::io::stdin(); + let mut stdin=stdin.lock(); + let mut u8b: [u8; 1] = [0]; + loop { + let mut buf: Vec = Vec::with_capacity(16); + loop { + let res = stdin.read(&mut u8b); + if res.unwrap_or(0) == 0 || u8b[0] <= b' ' { + break; + } else { + buf.push(u8b[0]); + } + } + if buf.len() >= 1 { + let ret = String::from_utf8(buf).unwrap(); + return ret; + } + } +} + +fn get() -> T { get_word().parse().ok().unwrap() } + +fn main() { + let n: i32 = get(); + let mut c = 0; + for _ in 0..n { + if get_word() == "Takahashi" { + c += 1; + } + } + println!("{}", c); +} diff --git a/atcoder/abc359/b.rs b/atcoder/abc359/b.rs new file mode 100644 index 00000000..3201d04d --- /dev/null +++ b/atcoder/abc359/b.rs @@ -0,0 +1,34 @@ +use std::io::Read; + +fn get_word() -> String { + let stdin = std::io::stdin(); + let mut stdin=stdin.lock(); + let mut u8b: [u8; 1] = [0]; + loop { + let mut buf: Vec = Vec::with_capacity(16); + loop { + let res = stdin.read(&mut u8b); + if res.unwrap_or(0) == 0 || u8b[0] <= b' ' { + break; + } else { + buf.push(u8b[0]); + } + } + if buf.len() >= 1 { + let ret = String::from_utf8(buf).unwrap(); + return ret; + } + } +} + +fn get() -> T { get_word().parse().ok().unwrap() } + +fn main() { + let n: usize = get(); + let mut occ = vec![vec![]; n]; + for i in 0..2 * n { + let x: usize = get(); + occ[x - 1].push(i); + } + println!("{}", (0..n).filter(|&i| occ[i][1] - occ[i][0] == 2).count()); +} diff --git a/atcoder/abc359/e.rs b/atcoder/abc359/e.rs new file mode 100644 index 00000000..9d260c9e --- /dev/null +++ b/atcoder/abc359/e.rs @@ -0,0 +1,64 @@ +use std::io::{Write, BufWriter}; +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, [ $t:tt ; $len:expr ]) => { + (0..$len).map(|_| read_value!($next, $t)).collect::>() + }; + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +fn main() { + let out = std::io::stdout(); + let mut out = BufWriter::new(out.lock()); + macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););} + macro_rules! putvec { + ($v:expr) => { + for i in 0..$v.len() { + puts!("{}{}", $v[i], if i + 1 == $v.len() {"\n"} else {" "}); + } + } + } + input! { + n: usize, + a: [i64; n], + } + let mut st = vec![(1 << 40, 0)]; + let mut tot = 0; + let mut ans = vec![]; + for i in 0..n { + while let Some(v) = st.pop() { + if v > (a[i], i + 1) { + st.push(v); + break; + } + tot -= v.0 * (v.1 - st[st.len() - 1].1) as i64; + } + st.push((a[i], i + 1)); + tot += a[i] * (i + 1 - st[st.len() - 2].1) as i64; + ans.push(tot + 1); + } + putvec!(ans); +} diff --git a/atcoder/abc359/f.rs b/atcoder/abc359/f.rs new file mode 100644 index 00000000..ffa7b28c --- /dev/null +++ b/atcoder/abc359/f.rs @@ -0,0 +1,51 @@ +use std::cmp::*; +use std::collections::*; +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, [ $t:tt ; $len:expr ]) => { + (0..$len).map(|_| read_value!($next, $t)).collect::>() + }; + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +fn main() { + input! { + n: usize, + a: [i64; n], + } + let mut que = BinaryHeap::new(); + let mut ans = 0; + for i in 0..n { + ans += a[i]; + que.push((Reverse(a[i] * 3), i, 2)); + } + for _ in 0..n - 2 { + let (Reverse(v), i, c) = que.pop().unwrap(); + ans += v; + que.push((Reverse(a[i] * (2 * c + 1)), i, c + 1)); + } + println!("{}", ans); +} diff --git a/atcoder/abc359/g.rs b/atcoder/abc359/g.rs new file mode 100644 index 00000000..7f71b947 --- /dev/null +++ b/atcoder/abc359/g.rs @@ -0,0 +1,93 @@ +use std::collections::*; +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) }; + ($next:expr, [ $t:tt ; $len:expr ]) => { + (0..$len).map(|_| read_value!($next, $t)).collect::>() + }; + ($next:expr, usize1) => (read_value!($next, usize) - 1); + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +fn main() { + // In order to avoid potential stack overflow, spawn a new thread. + let stack_size = 104_857_600; // 100 MB + let thd = std::thread::Builder::new().stack_size(stack_size); + thd.spawn(|| solve()).unwrap().join().unwrap(); +} + +type Dat = (i64 /* depth_delta */, HashMap); + +fn unite(mut a: Dat, mut b: Dat) -> (i64, Dat) { + if a.1.len() < b.1.len() { + std::mem::swap(&mut a, &mut b); + } + let (ad, mut a) = a; + let (bd, b) = b; + let mut sum = 0; + for (k, v) in b { + let ent = a.entry(k).or_insert((0, 0)); + sum += v.0 * (ent.1 + ad * ent.0); + sum += ent.0 * (v.1 + bd * v.0); + ent.0 += v.0; + ent.1 += v.1 + (bd - ad) * v.0; + } + (sum, (ad, a)) +} + +fn dfs(v: usize, par: usize, g: &[Vec], a: &[i32]) -> (i64, Dat) { + let mut hm = HashMap::new(); + hm.insert(a[v], (1, 0)); + let mut hm = (0, hm); + let mut sum = 0; + for &w in &g[v] { + if w == par { + continue; + } + let (subsum, mut sub) = dfs(w, v, g, a); + sum += subsum; + sub.0 += 1; + let (subsum, newhm) = unite(hm, sub); + hm = newhm; + sum += subsum; + } + (sum, hm) +} + +fn solve() { + input! { + n: usize, + uv: [(usize1, usize1); n - 1], + a: [i32; n], + } + let mut g = vec![vec![]; n]; + for &(u, v) in &uv { + g[u].push(v); + g[v].push(u); + } + let (sum, _) = dfs(0, n, &g, &a); + println!("{}", sum); +} diff --git a/atcoder/abc359/remain.txt b/atcoder/abc359/remain.txt new file mode 100644 index 00000000..18ebd852 --- /dev/null +++ b/atcoder/abc359/remain.txt @@ -0,0 +1,2 @@ +c +d diff --git a/yukicoder/2772.rs b/yukicoder/2772.rs new file mode 100644 index 00000000..5a55bca8 --- /dev/null +++ b/yukicoder/2772.rs @@ -0,0 +1,32 @@ +fn getline() -> String { + let mut ret = String::new(); + std::io::stdin().read_line(&mut ret).ok().unwrap(); + ret +} + +const MOD: i64 = 998_244_353; + +fn main() { + let s: Vec<_> = getline().trim().bytes().map(|b| (b - b'0') as usize).collect(); + let n = s.len(); + let mut dp = vec![[0; 2]; 1 << 10]; + for i in 0..n { + let mut ep = vec![[0; 2]; 1 << 10]; + // start + for d in 1..if i == 0 { s[0] + 1 } else { 10 } { + ep[1 << d][if i == 0 && d == s[0] { 1 } else { 0 }] += 1; + } + // continue + for bits in 0..1 << 10 { + for eq in 0..2 { + for d in 0..if eq == 1 { s[i] + 1 } else { 10 } { + let neq = if eq == 1 && d == s[i] { 1 } else { 0 }; + ep[bits ^ 1 << d][neq] += dp[bits][eq]; + ep[bits ^ 1 << d][neq] %= MOD; + } + } + } + dp = ep; + } + println!("{}", (dp[0][0] + dp[0][1]) % MOD); +}