diff --git a/atcoder/abc353/a.rs b/atcoder/abc353/a.rs new file mode 100644 index 00000000..a27e50ab --- /dev/null +++ b/atcoder/abc353/a.rs @@ -0,0 +1,37 @@ +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 h1: i32 = get(); + for i in 2..=n { + let x: i32 = get(); + if x > h1 { + println!("{}", i); + return; + } + } + println!("-1"); +} diff --git a/atcoder/abc353/b.rs b/atcoder/abc353/b.rs new file mode 100644 index 00000000..6f894bfd --- /dev/null +++ b/atcoder/abc353/b.rs @@ -0,0 +1,41 @@ +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 k: i32 = get(); + let mut rem = k; + let mut ans = 0; + for _ in 0..n { + let a: i32 = get(); + if rem < a { + ans += 1; + rem = k - a; + } else { + rem -= a; + } + } + println!("{}", ans + 1); +} diff --git a/atcoder/abc353/c.rs b/atcoder/abc353/c.rs new file mode 100644 index 00000000..dc899c79 --- /dev/null +++ b/atcoder/abc353/c.rs @@ -0,0 +1,82 @@ +// 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")); +} + +trait Bisect { + fn lower_bound(&self, val: &T) -> usize; + fn upper_bound(&self, val: &T) -> usize; +} + +impl Bisect for [T] { + fn lower_bound(&self, val: &T) -> usize { + let mut pass = self.len() + 1; + let mut fail = 0; + while pass - fail > 1 { + let mid = (pass + fail) / 2; + if &self[mid - 1] >= val { + pass = mid; + } else { + fail = mid; + } + } + pass - 1 + } + fn upper_bound(&self, val: &T) -> usize { + let mut pass = self.len() + 1; + let mut fail = 0; + while pass - fail > 1 { + let mid = (pass + fail) / 2; + if &self[mid - 1] > val { + pass = mid; + } else { + fail = mid; + } + } + pass - 1 + } +} + +fn main() { + input! { + n: usize, + a: [i64; n], + } + let mut sum: i64 = a.iter().sum(); + sum *= n as i64 - 1; + const MOD: i64 = 100_000_000; + let mut a = a; + a.sort(); + for i in 0..n { + let val = a[i]; + let idx = a.lower_bound(&(MOD - val)); + sum -= MOD * (n - idx.max(i + 1)) as i64; + } + println!("{}", sum); +} diff --git a/atcoder/abc353/d.rs b/atcoder/abc353/d.rs new file mode 100644 index 00000000..8b91397c --- /dev/null +++ b/atcoder/abc353/d.rs @@ -0,0 +1,51 @@ +// 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 sum = 0; + const MOD: i64 = 998_244_353; + let mut coef = 0; + for i in (0..n).rev() { + sum += a[i] * i as i64; + sum += a[i] * coef; + sum %= MOD; + let mut d = 1; + while d <= a[i] { + d *= 10; + } + coef = (coef + d) % MOD; + } + println!("{}", sum); +} diff --git a/atcoder/abc353/e.rs b/atcoder/abc353/e.rs new file mode 100644 index 00000000..9c6f3603 --- /dev/null +++ b/atcoder/abc353/e.rs @@ -0,0 +1,87 @@ +// 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, chars) => { + read_value!($next, String).chars().collect::>() + }; + ($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(); +} + +fn rec(s: &[Vec], pos: usize) -> i64 { + let n = s.len(); + if n == 0 { + return 0; + } + let mut ans = 0; + let mut pass = s.len() + 1; + let mut fail = 0; + while pass - fail > 1 { + let mid = (pass + fail) / 2; + if s[mid - 1].len() <= pos { + fail = mid; + } else { + pass = mid; + } + } + let mut last = pass - 1; + for c in 'a'..='z' { + let mut pass = s.len() + 1; + let mut fail = last; + while pass - fail > 1 { + let mid = (pass + fail) / 2; + if s[mid - 1].len() <= pos || s[mid - 1][pos] <= c { + fail = mid; + } else { + pass = mid; + } + } + ans += rec(&s[last..pass - 1], pos + 1); + let len = (pass - last - 1) as i64; + ans += len * (len - 1) / 2; + last = pass - 1; + } + ans +} + +// Tags: trie-less +fn solve() { + input! { + n: usize, + s: [chars; n], + } + let mut s = s; + s.sort(); + println!("{}", rec(&s, 0)); +} diff --git a/atcoder/abc353/remain.txt b/atcoder/abc353/remain.txt new file mode 100644 index 00000000..ab713757 --- /dev/null +++ b/atcoder/abc353/remain.txt @@ -0,0 +1,2 @@ +f +g