Skip to content

Commit

Permalink
Add atcoder/abc352/c.rs atcoder/abc352/d.rs atcoder/abc352/e.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
koba-e964 committed May 5, 2024
1 parent 3ef59f2 commit 306d7dd
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 3 deletions.
37 changes: 37 additions & 0 deletions atcoder/abc352/c.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: usize = get();
let mut tot = 0;
let mut ma = 0;
for _ in 0..n {
let a: i64 = get();
let b: i64 = get();
tot += a;
ma = ma.max(b - a);
}
println!("{}", tot + ma);
}
110 changes: 110 additions & 0 deletions atcoder/abc352/d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// 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, usize1) => (read_value!($next, usize) - 1);
($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

// 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() {
input! {
n: usize, k: usize,
p: [usize; n],
}
let mut pinv = vec![0; n];
for i in 0..n {
pinv[p[i] - 1] = i;
}
let mut ma = SegTree::new(n, |x, y| x.max(y), 0);
let mut mi = SegTree::new(n, |x, y| x.min(y), n);
for i in 0..n {
ma.update(i, pinv[i]);
mi.update(i, pinv[i]);
}
let mut ans = n;
for i in 0..n - k + 1 {
let hi = ma.query(i..i + k);
let lo = mi.query(i..i + k);
ans = ans.min(hi - lo);
}
println!("{}", ans);
}
101 changes: 101 additions & 0 deletions atcoder/abc352/e.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// 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, [ $t:tt ]) => {{
let len = read_value!($next, usize);
read_value!($next, [$t; len])
}};
($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

// Union-Find tree.
// Verified by https://atcoder.jp/contests/pakencamp-2019-day3/submissions/9253305
struct UnionFind { disj: Vec<usize>, rank: Vec<usize> }

impl UnionFind {
fn new(n: usize) -> Self {
let disj = (0..n).collect();
UnionFind { disj: disj, rank: vec![1; n] }
}
fn root(&mut self, x: usize) -> usize {
if x != self.disj[x] {
let par = self.disj[x];
let r = self.root(par);
self.disj[x] = r;
}
self.disj[x]
}
fn unite(&mut self, x: usize, y: usize) {
let mut x = self.root(x);
let mut y = self.root(y);
if x == y { return }
if self.rank[x] > self.rank[y] {
std::mem::swap(&mut x, &mut y);
}
self.disj[x] = y;
self.rank[y] += self.rank[x];
}
#[allow(unused)]
fn is_same_set(&mut self, x: usize, y: usize) -> bool {
self.root(x) == self.root(y)
}
#[allow(unused)]
fn size(&mut self, x: usize) -> usize {
let x = self.root(x);
self.rank[x]
}
}

fn main() {
input! {
n: usize, m: usize,
dat: [([i64], i64); m],
}
let mut s = vec![];
for (mut d, e) in dat {
d.push(e);
s.push((d[0], d[1..].to_vec()));
}
s.sort();
let mut uf = UnionFind::new(n);
let mut ans = 0;
let mut conn = n;
for (wei, cli) in s {
for i in 1..cli.len() {
let x = cli[0] as usize - 1;
let y = cli[i] as usize - 1;
if !uf.is_same_set(x, y) {
uf.unite(x, y);
ans += wei;
conn -= 1;
}
}
}
println!("{}", if conn != 1 { -1 } else { ans });
}
3 changes: 0 additions & 3 deletions atcoder/abc352/remain.txt

This file was deleted.

0 comments on commit 306d7dd

Please sign in to comment.