Skip to content

Commit

Permalink
Add atcoder/abc351/a.rs atcoder/abc351/b.rs atcoder/abc351/c.rs atcod…
Browse files Browse the repository at this point in the history
…er/abc351/d.rs atcoder/abc351/e.rs atcoder/abc351/f.rs atcoder/abc351/remain.txt
  • Loading branch information
koba-e964 committed Apr 28, 2024
1 parent 7ad8871 commit eae05f4
Show file tree
Hide file tree
Showing 7 changed files with 432 additions and 0 deletions.
40 changes: 40 additions & 0 deletions atcoder/abc351/a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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! {
a: [i32; 9],
b: [i32; 8],
}
let asum: i32 = a.iter().sum();
let bsum: i32 = b.iter().sum();
println!("{}", asum - bsum + 1);
}
49 changes: 49 additions & 0 deletions atcoder/abc351/b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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() {
input! {
n: usize,
a: [chars; n],
b: [chars; n],
}
for i in 0..n {
for j in 0..n {
if a[i][j] != b[i][j] {
println!("{} {}", i + 1, j + 1);
return;
}
}
}
}
47 changes: 47 additions & 0 deletions atcoder/abc351/c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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: [i32; n],
}
let mut s = vec![];
for a in a {
s.push(a);
while s.len() >= 2 && s[s.len() - 2] == s[s.len() - 1] {
s.pop();
let x = s.pop().unwrap();
s.push(x + 1);
}
}
println!("{}", s.len());
}
115 changes: 115 additions & 0 deletions atcoder/abc351/d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// 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"));
}

// 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! {
h: usize, w: usize,
s: [chars; h],
}
let mut ma = 1;
let mut uf = UnionFind::new(5 * h * w);
let dxy = [(0i32, 1i32), (1, 0), (0, -1), (-1, 0)];
for x in 0..h {
for y in 0..w {
if s[x][y] == '#' { continue; }
let mut has = false;
for &(dx, dy) in &dxy {
let nx = x.wrapping_add(dx as usize);
let ny = y.wrapping_add(dy as usize);
if nx >= h || ny >= w { continue; }
if s[nx][ny] == '#' {
has = true;
}
}
if has { continue; }
let v = x * w + y;
for dir in 0..4 {
let (dx, dy) = dxy[dir];
let nx = x.wrapping_add(dx as usize);
let ny = y.wrapping_add(dy as usize);
if nx >= h || ny >= w { continue; }
uf.unite(5 * v + dir, 5 * (nx * w + ny) + ((dir + 2) % 4));
uf.unite(5 * v + 4, 5 * v + dir);
}
}
}
let mut size = vec![vec![]; 5 * h * w];
for i in 0..5 * h * w {
size[uf.root(i)].push(i / 5);
}
for i in 0..5 * h * w {
size[i].sort_unstable();
size[i].dedup();
ma = ma.max(size[i].len());
}
println!("{}", ma);
}
72 changes: 72 additions & 0 deletions atcoder/abc351/e.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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:ty) => ($next().parse::<$t>().expect("Parse error"));
}

fn g(a: Vec<i64>) -> i64 {
let n = a.len() as i64;
let mut tot = 0;
for i in 0..n {
tot += a[i as usize] * (2 * i - n + 1);
}
tot
}

fn f(a: &[(i64, i64)]) -> i64 {
if a.is_empty() {
return 0;
}
let mut bx = vec![];
let mut by = vec![];
let c = (a[0].0 + a[0].1) & 1;
for &(x, y) in a {
bx.push((x + y - c) / 2);
by.push((x - y + c) / 2);
}
bx.sort(); by.sort();
g(bx) + g(by)
}

fn main() {
input! {
n: usize,
xy: [(i64, i64); n],
}
let mut even = vec![];
let mut odd = vec![];
for i in 0..n {
if (xy[i].0 + xy[i].1) % 2 == 0 {
even.push(xy[i]);
} else {
odd.push(xy[i]);
}
}
println!("{}", f(&even) + f(&odd));
}
Loading

0 comments on commit eae05f4

Please sign in to comment.