Skip to content

Commit

Permalink
Add atcoder/abc340/a.rs atcoder/abc340/b.rs atcoder/abc340/c.rs atcod…
Browse files Browse the repository at this point in the history
…er/abc340/d.rs atcoder/abc340/remain.txt
  • Loading branch information
koba-e964 committed Apr 26, 2024
1 parent 26b0439 commit 5db4ee7
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 3 deletions.
34 changes: 34 additions & 0 deletions atcoder/abc340/a.rs
Original file line number Diff line number Diff line change
@@ -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<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 mut a: i32 = get();
let b: i32 = get();
let d: i32 = get();
while a <= b {
print!("{} ", a);
a += d;
}
}
38 changes: 38 additions & 0 deletions atcoder/abc340/b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 q: i32 = get();
let mut v = vec![];
for _ in 0..q {
let ty: i32 = get();
let c: usize = get();
if ty == 1 {
v.push(c);
} else {
println!("{}", v[v.len() - c]);
}
}
}
40 changes: 40 additions & 0 deletions atcoder/abc340/c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 mut n: u64 = get();
let mut c = 0;
let mut d = 1;
let mut tot = 0;
while n > 1 {
tot += n * d + c;
if n % 2 == 1 {
c = d + c;
}
n /= 2;
d *= 2;
}
println!("{}", tot + c * 2);
}
85 changes: 85 additions & 0 deletions atcoder/abc340/d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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::<Vec<_>>()
};
($next:expr, usize1) => (read_value!($next, usize) - 1);
($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

// Dijkstra's algorithm.
// Verified by: AtCoder ABC164 (https://atcoder.jp/contests/abc164/submissions/12423853)
struct Dijkstra {
edges: Vec<Vec<(usize, i64)>>, // adjacent list representation
}

impl Dijkstra {
fn new(n: usize) -> Self {
Dijkstra { edges: vec![Vec::new(); n] }
}
fn add_edge(&mut self, from: usize, to: usize, cost: i64) {
self.edges[from].push((to, cost));
}
// This function returns a Vec consisting of the distances from vertex source.
fn solve(&self, source: usize, inf: i64) -> Vec<i64> {
let n = self.edges.len();
let mut d = vec![inf; n];
// que holds (-distance, vertex), so that que.pop() returns the nearest element.
let mut que = std::collections::BinaryHeap::new();
que.push((0, source));
while let Some((cost, pos)) = que.pop() {
let cost = -cost;
if d[pos] <= cost {
continue;
}
d[pos] = cost;
for &(w, c) in &self.edges[pos] {
let newcost = cost + c;
if d[w] > newcost {
d[w] = newcost + 1;
que.push((-newcost, w));
}
}
}
return d;
}
}

fn main() {
input! {
n: usize,
abx: [(i64, i64, usize1); n - 1],
}
let mut dijk = Dijkstra::new(n);
for i in 0..n - 1 {
let (a, b, x) = abx[i];
dijk.add_edge(i, i + 1, a);
dijk.add_edge(i, x, b);
}
let sol = dijk.solve(0, 1i64 << 60);
println!("{}", sol[n - 1]);
}
3 changes: 3 additions & 0 deletions atcoder/abc340/remain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
e
f
g
4 changes: 1 addition & 3 deletions comm/graph/Dijkstra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ impl Dijkstra {
fn add_edge(&mut self, from: usize, to: usize, cost: i64) {
self.edges[from].push((to, cost));
}
/*
* This function returns a Vec consisting of the distances from vertex source.
*/
// This function returns a Vec consisting of the distances from vertex source.
fn solve(&self, source: usize, inf: i64) -> Vec<i64> {
let n = self.edges.len();
let mut d = vec![inf; n];
Expand Down

0 comments on commit 5db4ee7

Please sign in to comment.