Skip to content

Commit

Permalink
Add atcoder/abc353/a.rs atcoder/abc353/b.rs atcoder/abc353/c.rs atcod…
Browse files Browse the repository at this point in the history
…er/abc353/d.rs atcoder/abc353/e.rs atcoder/abc353/remain.txt
  • Loading branch information
koba-e964 committed Jun 18, 2024
1 parent 1516d87 commit d4ee856
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 0 deletions.
37 changes: 37 additions & 0 deletions atcoder/abc353/a.rs
Original file line number Diff line number Diff line change
@@ -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<u8> = 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: std::str::FromStr>() -> 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");
}
41 changes: 41 additions & 0 deletions atcoder/abc353/b.rs
Original file line number Diff line number Diff line change
@@ -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<u8> = 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: std::str::FromStr>() -> 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);
}
82 changes: 82 additions & 0 deletions atcoder/abc353/c.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<_>>()
};
($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Bisect<T> {
fn lower_bound(&self, val: &T) -> usize;
fn upper_bound(&self, val: &T) -> usize;
}

impl<T: Ord> Bisect<T> 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);
}
51 changes: 51 additions & 0 deletions atcoder/abc353/d.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<_>>()
};
($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);
}
87 changes: 87 additions & 0 deletions atcoder/abc353/e.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<_>>()
};
($next:expr, chars) => {
read_value!($next, String).chars().collect::<Vec<char>>()
};
($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<char>], 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));
}
2 changes: 2 additions & 0 deletions atcoder/abc353/remain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
f
g

0 comments on commit d4ee856

Please sign in to comment.