Skip to content

Commit

Permalink
Add atcoder/abc342/b.rs atcoder/abc342/c.rs atcoder/abc342/d.rs atcod…
Browse files Browse the repository at this point in the history
…er/abc342/e.rs atcoder/abc342/f.rs
  • Loading branch information
koba-e964 committed May 1, 2024
1 parent b9d58c0 commit d528c6b
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 5 deletions.
39 changes: 39 additions & 0 deletions atcoder/abc342/b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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: usize = get();
let mut inv = vec![0; n + 1];
for i in 0..n {
let a: usize = get();
inv[a] = i;
}
let q: i32 = get();
for _ in 0..q {
let a: usize = get();
let b: usize = get();
println!("{}", if inv[a] < inv[b] { a } else { b });
}
}
48 changes: 48 additions & 0 deletions atcoder/abc342/c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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: usize = get();
let s = get_word().bytes().collect::<Vec<_>>();
let mut tbl = vec![0; 26];
for i in 0..26 {
tbl[i] = i;
}
let q: i32 = get();
for _ in 0..q {
let c: char = get();
let d: char = get();
let c = c as usize - 'a' as usize;
let d = d as usize - 'a' as usize;
for i in 0..26 {
if tbl[i] == c {
tbl[i] = d;
}
}
}
for c in s {
print!("{}", (tbl[(c - b'a') as usize] as u8 + b'a') as char);
}
}
58 changes: 58 additions & 0 deletions atcoder/abc342/d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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() }

// Tags: square-free-part
fn main() {
let n: usize = get();
const W: usize = 200_100;
let mut sqf = vec![0; W];
for i in 1..W {
sqf[i] = i;
}
for i in 2..W {
if i * i >= W {
break;
}
for j in 1..(W - 1) / (i * i) + 1 {
while sqf[j * i * i] % (i * i) == 0 {
sqf[j * i * i] /= i * i;
}
}
}
let mut zero = 0i64;
let mut hm = std::collections::HashMap::new();
let mut tot = 0i64;
for _ in 0..n {
let a: usize = get();
if a == 0 {
zero += 1;
continue;
}
*hm.entry(sqf[a]).or_insert(0) += 1;
tot += hm[&sqf[a]] - 1;
}
let nn = n as i64;
println!("{}", tot + zero * (nn - zero) + zero * (zero - 1) / 2);
}
76 changes: 76 additions & 0 deletions atcoder/abc342/e.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::collections::*;
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),* )) => { ($(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"));
}

fn main() {
let out = std::io::stdout();
let mut out = BufWriter::new(out.lock());
macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
input! {
n: usize, m: usize,
ldkcab: [(i64, i64, i64, i64, usize1, usize1); m],
}
let mut g = vec![vec![]; n];
for &(l, d, k, c, a, b) in &ldkcab {
g[b].push((a, l, d, k, c));
}
let mut que = BinaryHeap::new();
const INF: i64 = 1 << 60;
que.push((INF, n - 1));
let mut dist = vec![-INF; n];
while let Some((time, v)) = que.pop() {
if dist[v] > time {
continue;
}
dist[v] = time;
for &(u, l, d, k, c) in &g[v] {
if l + c > time {
continue;
}
let q = ((time - l - c) / d).min(k - 1);
let ntime = l + d * q;
if dist[u] >= ntime {
continue;
}
que.push((ntime, u));
}
}
for i in 0..n - 1 {
if dist[i] == -INF {
puts!("Unreachable\n");
} else {
puts!("{}\n", dist[i]);
}
}
}
120 changes: 120 additions & 0 deletions atcoder/abc342/f.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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() }

// Segment Tree. This data structure is useful for fast folding on intervals of an array
// whose elements are elements of monoid I. Note that constructing this tree requires the identity
// element of I and the operation of I.
// Verified by: yukicoder No. 2220 (https://yukicoder.me/submissions/841554)
struct SegTree<I, BiOp> {
n: usize,
orign: usize,
dat: Vec<I>,
op: BiOp,
e: I,
}

impl<I, BiOp> SegTree<I, BiOp>
where BiOp: Fn(I, I) -> I,
I: Copy {
pub fn new(n_: usize, op: BiOp, e: I) -> Self {
let mut n = 1;
while n < n_ { n *= 2; } // n is a power of 2
SegTree {n: n, orign: n_, dat: vec![e; 2 * n - 1], op: op, e: e}
}
// ary[k] <- v
pub fn update(&mut self, idx: usize, v: I) {
debug_assert!(idx < self.orign);
let mut k = idx + self.n - 1;
self.dat[k] = v;
while k > 0 {
k = (k - 1) / 2;
self.dat[k] = (self.op)(self.dat[2 * k + 1], self.dat[2 * k + 2]);
}
}
// [a, b) (half-inclusive)
// http://proc-cpuinfo.fixstars.com/2017/07/optimize-segment-tree/
#[allow(unused)]
pub fn query(&self, rng: std::ops::Range<usize>) -> I {
let (mut a, mut b) = (rng.start, rng.end);
debug_assert!(a <= b);
debug_assert!(b <= self.orign);
let mut left = self.e;
let mut right = self.e;
a += self.n - 1;
b += self.n - 1;
while a < b {
if (a & 1) == 0 {
left = (self.op)(left, self.dat[a]);
}
if (b & 1) == 0 {
right = (self.op)(self.dat[b - 1], right);
}
a = a / 2;
b = (b - 1) / 2;
}
(self.op)(left, right)
}
}

fn main() {
let n: usize = get();
let l: usize = get();
let d: usize = get();
let mut deal_imos = SegTree::new(n + 3, |x, y| x + y, 0.0);
deal_imos.update(0, 1.0);
deal_imos.update(1, -1.0);
for i in 0..l {
let rg = deal_imos.query(0..i + 1);
let val = rg / d as f64;
// deal[i + 1..i + 1 + d] += val
let targ = (n + 2).min(i + d + 1);
let rem = i + d + 1 - targ;
let tmp = deal_imos.query(targ..targ + 1);
deal_imos.update(targ, tmp - val);
let tmp = deal_imos.query(i + 1..i + 2);
deal_imos.update(i + 1, tmp + val);
if rem > 0 {
let tmp = deal_imos.query(n + 1..n + 2);
deal_imos.update(n + 1, tmp + val * rem as f64);
}
}
let mut deal = vec![0.0; n + 2];
for i in l..n + 2 {
deal[i] = deal_imos.query(0..i + 1);
}
let mut deal_acc = vec![0.0; n + 3];
for i in 0..n + 2 {
deal_acc[i + 1] = deal_acc[i] + deal[i];
}

let mut dp = SegTree::new(n + 2, |x, y| x + y, 0.0);
for i in (0..n + 1).rev() {
let mut me = deal_acc[i] + deal[n + 1];
let tmp = dp.query(i + 1..(i + d + 1).min(n + 2)) / d as f64;
me = me.max(tmp);
dp.update(i, me);
}
println!("{}", dp.query(0..1));
}
5 changes: 0 additions & 5 deletions atcoder/abc342/remain.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
b
c
d
e
f
g

0 comments on commit d528c6b

Please sign in to comment.