Skip to content

Commit

Permalink
Add yukicoder/2456.rs yukicoder/2458.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
koba-e964 committed Sep 10, 2023
1 parent f4033db commit 338bf12
Show file tree
Hide file tree
Showing 2 changed files with 264 additions and 0 deletions.
90 changes: 90 additions & 0 deletions yukicoder/2456.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::cmp::*;
// 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! {
h: usize, w: usize,
s: [chars; h],
}
let mut acc = vec![vec![0; w + 1]; h + 1];
for i in 0..h {
for j in 0..w {
acc[i + 1][j + 1] = acc[i + 1][j] + acc[i][j + 1] - acc[i][j]
+ if s[i][j] == '.' {
1
} else {
0
};
}
}
let mut fail = min(h, w) + 1;
let mut pass = 1;
while fail - pass > 1 {
let mid = (fail + pass) / 2;
let mut imos = vec![vec![0; w + 1]; h + 1];
for i in 0..h + 1 - mid {
for j in 0..w + 1 - mid {
if acc[i + mid][j + mid] + acc[i][j] == acc[i + mid][j] + acc[i][j + mid] {
imos[i][j] += 1;
imos[i][j + mid] -= 1;
imos[i + mid][j] -= 1;
imos[i + mid][j + mid] += 1;
}
}
}
for i in 0..h + 1 {
for j in 0..w {
imos[i][j + 1] += imos[i][j];
}
}
for i in 0..h {
for j in 0..w + 1 {
imos[i + 1][j] += imos[i][j];
}
}
let mut ok = true;
for i in 0..h {
for j in 0..w {
ok &= (imos[i][j] != 0) == (s[i][j] == '#');
}
}
if ok {
pass = mid;
} else {
fail = mid;
}
}
println!("{}", pass);
}
174 changes: 174 additions & 0 deletions yukicoder/2458.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
use std::cmp::*;
// 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"));
}

// https://github.com/kth-competitive-programming/kactl/blob/main/content/data-structures/LineContainer.h
// Verified by: https://judge.yosupo.jp/submission/63195
type Coord = i64;

#[derive(Clone, Debug, Default)]
struct MinCHT {
lines: std::collections::BTreeSet<Entry>,
}

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
struct Point(Coord);

#[derive(Eq, Debug, Clone)]
struct Entry(Coord, Coord, std::cell::Cell<Point>);
impl PartialEq for Entry {
fn eq(&self, o: &Self) -> bool {
(self.0, self.1) == (o.0, o.1)
}
}
impl PartialOrd for Entry {
fn partial_cmp(&self, o: &Self) -> Option<std::cmp::Ordering> {
(o.0, o.1).partial_cmp(&(self.0, self.1))
}
}
impl Ord for Entry {
fn cmp(&self, o: &Self) -> std::cmp::Ordering {
(o.0, o.1).cmp(&(self.0, self.1))
}
}
impl std::borrow::Borrow<Point> for Entry {
fn borrow(&self) -> &Point {
unsafe { &*self.2.as_ptr() }
}
}

impl MinCHT {
const INF: i64 = 1 << 62;
fn div(a: Coord, b: Coord) -> Coord {
a / b - if (a ^ b) < 0 && a % b != 0 { 1 } else { 0 }
}
// Should we erase y from lines?
// As well as modifying x.2 to the appropriate value
fn isect(x: &Entry, y: Option<&Entry>) -> bool {
let y = if let Some(y) = y {
y
} else {
x.2.set(Point(Self::INF));
return false;
};
assert!(!std::ptr::eq(&x.2, &y.2));
if x.0 == y.0 {
let p = if x.1 < y.1 {
Self::INF
} else {
-Self::INF
};
x.2.set(Point(p));
} else {
x.2.set(Point(Self::div(y.1 - x.1, x.0 - y.0)));
}
x.2 >= y.2
}
fn new() -> Self {
Default::default()
}
// Adds y = ax + b
fn add(&mut self, a: Coord, b: Coord) {
let e = Entry(a, b, std::cell::Cell::new(Point(0)));
if self.lines.contains(&e) {
return;
}
self.lines.insert(e.clone());
loop {
let y = self.lines.get(&e).unwrap();
let z = self.lines.range(Entry(a, b - 1, std::cell::Cell::new(Point(0)))..).next();
if Self::isect(y, z) {
let z = z.unwrap().clone();
self.lines.remove(&z);
} else {
break;
}
}
let mut now;
{
let y = self.lines.range(e.clone()..).next();
let x = self.lines.range(..e.clone()).rev().next();
if let Some(x) = x {
now = x.clone();
if Self::isect(x, y) {
let y = y.unwrap().clone();
self.lines.remove(&y);
let xx = self.lines.range(..e.clone()).rev().next().unwrap();
let yy = self.lines.range(e.clone()..).next();
Self::isect(xx, yy);
}
} else {
return;
}
}
loop {
let y = self.lines.range(now.clone()..).next();
let x = self.lines.range(..now.clone()).rev().next();
if let Some(x) = x {
if Self::isect(x, y) {
let x = x.clone();
let y = y.unwrap().clone();
self.lines.remove(&y);
let xx = self.lines.range(..now.clone()).rev().next().unwrap();
let yy = self.lines.range(now.clone()..).next();
Self::isect(xx, yy);
now = x;
continue;
}
}
break;
}
}
fn query(&self, x: Coord) -> Coord {
assert!(!self.lines.is_empty());
let &Entry(a, b, _) = self.lines.range(Point(x)..).next().unwrap();
a * x + b
}
}

fn main() {
input! {
n: usize,
a: [i64; n],
}
let mut dp = vec![0; n];
let mut cht = MinCHT::new();
for i in 0..n {
dp[i] = if i > 0 {
max(0, -cht.query(a[i]))
} else {
0
};
cht.add(-a[i], -dp[i]);
}
let &ans = dp.iter().max().unwrap();
println!("{}", ans);
}

0 comments on commit 338bf12

Please sign in to comment.