Skip to content

Commit

Permalink
Timus 1490, 2069
Browse files Browse the repository at this point in the history
  • Loading branch information
907th committed Apr 11, 2023
1 parent 69cf96b commit c86ef1c
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Timus/1490/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SRC=solution.rs
EXE=solution.exe

.PHONY: debug
debug:
rustc -o ${EXE} ${SRC}
./${EXE}

.PHONY: release
release:
rustc -O -o ${EXE} ${SRC}
./${EXE}

.PHONY: test
test:
rustc --test -o ${EXE} ${SRC}
./${EXE} --nocapture

124 changes: 124 additions & 0 deletions Timus/1490/solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#![allow(unused_imports)]

use std::cmp::{max, min, Ordering};
use std::fmt::{Debug, Display};
use std::io::{BufRead, BufReader, BufWriter, Read, Write};
use std::collections::{VecDeque, HashMap, HashSet, BinaryHeap};
use std::ops::{Neg, Mul, Add, Sub, Shl};
use std::mem;

fn solve(r: u64) -> u64 {
let mut ans: u64 = 0;
for i in 0..r {
let j = ((r * r - i * i) as f64).sqrt() as u64;
if r * r == i * i + j * j { ans += j; } else { ans += j + 1; }
}
ans * 4
}

fn read_eval_print<R: Read, W: Write>(io: &mut IO<R, W>) -> IOResult<()> {
let r: u64 = io.ln()?;
let ans = solve(r);
writeln!(io, "{}", ans)?;
Ok(())
}

fn main() -> IOResult<()> {
let mut io = IO::new(std::io::stdin(), std::io::stdout());
read_eval_print(&mut io)
}

// I/O

type IOResult<T> = Result<T, Box<dyn std::error::Error>>;

struct IO<R: Read, W: Write> {
r: BufReader<R>,
w: BufWriter<W>
}

#[allow(dead_code)]
impl<R: Read, W: Write> IO<R, W> {
fn new(r: R, w: W) -> Self {
Self{ r: BufReader::new(r), w: BufWriter::new(w) }
}

fn bytes(&mut self, n: usize) -> IOResult<Vec<u8>> {
let mut buf = vec![0u8; n];
self.r.read_exact(&mut buf)?;
Ok(buf)
}

fn byte(&mut self) -> IOResult<u8> { Ok(self.bytes(1)?[0]) }

fn eof(&mut self) -> IOResult<String> {
let mut str = String::new();
self.r.read_to_string(&mut str)?;
Ok(str)
}

fn parse_until<T>(&mut self, byte: u8, trim: &[char]) -> IOResult<T> where T: std::str::FromStr, <T as std::str::FromStr>::Err: std::error::Error + 'static {
let mut buf = Vec::new();
self.r.read_until(byte, &mut buf)?;
let str = String::from_utf8(buf)?;
Ok(str.trim_end_matches(trim).parse()?)
}

fn ln<T>(&mut self) -> IOResult<T> where T: std::str::FromStr, <T as std::str::FromStr>::Err: std::error::Error + 'static {
self.parse_until(0xA, &[0xD as char, 0xA as char]) // Trim both \r and \n on Windows
}

fn sp<T>(&mut self) -> IOResult<T> where T: std::str::FromStr, <T as std::str::FromStr>::Err: std::error::Error + 'static {
self.parse_until(0x20, &[0x20 as char])
}
}

#[allow(dead_code)]
fn split_sp<T>(s: &str) -> IOResult<Vec<T>> where T: std::str::FromStr, <T as std::str::FromStr>::Err: std::error::Error + 'static {
let mut vec = Vec::new();
for part in s.split_whitespace() {
vec.push(part.parse()?);
}
Ok(vec)
}

// IO delegates all Write methods to self.w
impl<R: Read, W: Write> Write for IO<R, W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { self.w.write(buf) }
fn flush(&mut self) -> std::io::Result<()> { self.w.flush() }
}

// Tests

#[cfg(test)]
mod test {
use std::fs::{self, File};
use super::{IO, IOResult, read_eval_print};

#[test]
fn run_tests() -> IOResult<()> {
let mut t = 0;
loop {
t += 1;
let in_path = format!("tests/{}.in", t);
let out_path = format!("tests/{}.out", t);
match File::open(in_path) {
Ok(r) => {
println!("Sample {}", t);
let w: Vec<u8> = Vec::new();
let mut io = IO::new(r, w);
read_eval_print(&mut io)?;
let out_received_bytes = io.w.into_inner()?;
let out_received = std::str::from_utf8(&out_received_bytes)?;
let out_expected_bytes = fs::read(out_path)?;
let out_expected = std::str::from_utf8(&out_expected_bytes)?;
assert_eq!(out_expected, out_received, "Wrong answer");
}
Err(_) => break
}
}
assert!(t > 1, "No samples were found");
println!("Total run {} samples", t - 1);
Ok(())
}
}
1 change: 1 addition & 0 deletions Timus/1490/tests/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
1 change: 1 addition & 0 deletions Timus/1490/tests/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16
1 change: 1 addition & 0 deletions Timus/1490/tests/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
1 change: 1 addition & 0 deletions Timus/1490/tests/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
60
18 changes: 18 additions & 0 deletions Timus/2069/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SRC=solution.rs
EXE=solution.exe

.PHONY: debug
debug:
rustc -o ${EXE} ${SRC}
./${EXE}

.PHONY: release
release:
rustc -O -o ${EXE} ${SRC}
./${EXE}

.PHONY: test
test:
rustc --test -o ${EXE} ${SRC}
./${EXE} --nocapture

141 changes: 141 additions & 0 deletions Timus/2069/solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#![allow(unused_imports)]

use std::cmp::{max, min, Ordering};
use std::fmt::{Debug, Display};
use std::io::{BufRead, BufReader, BufWriter, Read, Write};
use std::collections::{VecDeque, HashMap, HashSet, BinaryHeap};
use std::ops::{Neg, Mul, Add, Sub, Shl};
use std::mem;

fn solve(n: usize, m: usize, v: Vec<u64>, h: Vec<u64>) -> u64 {
let mut ans: u64 = 0;
ans = max(ans, min(v[0], h[m - 1])); // down -> right
ans = max(ans, min(h[0], v[n - 1])); // right -> down
if m > 2 {
let hh: u64 = *h[1..(m - 1)].iter().max().unwrap();
ans = max(ans, min(min(v[0], v[n - 1]), hh)); // down -> right -> down
}
if n > 2 {
let vv: u64 = *v[1..(n - 1)].iter().max().unwrap();
ans = max(ans, min(min(h[0], h[m - 1]), vv)); // right -> down -> right
}
ans
}

fn read_eval_print<R: Read, W: Write>(io: &mut IO<R, W>) -> IOResult<()> {
let n = io.sp()?;
let m = io.ln()?;
let mut v: Vec<u64> = Vec::new();
for _ in 0..n {
let x = io.ln()?;
v.push(x);
}
let mut h: Vec<u64> = Vec::new();
for _ in 0..m {
let x = io.ln()?;
h.push(x);
}
let ans = solve(n, m, v, h);
writeln!(io, "{}", ans)?;
Ok(())
}

fn main() -> IOResult<()> {
let mut io = IO::new(std::io::stdin(), std::io::stdout());
read_eval_print(&mut io)
}

// I/O

type IOResult<T> = Result<T, Box<dyn std::error::Error>>;

struct IO<R: Read, W: Write> {
r: BufReader<R>,
w: BufWriter<W>
}

#[allow(dead_code)]
impl<R: Read, W: Write> IO<R, W> {
fn new(r: R, w: W) -> Self {
Self{ r: BufReader::new(r), w: BufWriter::new(w) }
}

fn bytes(&mut self, n: usize) -> IOResult<Vec<u8>> {
let mut buf = vec![0u8; n];
self.r.read_exact(&mut buf)?;
Ok(buf)
}

fn byte(&mut self) -> IOResult<u8> { Ok(self.bytes(1)?[0]) }

fn eof(&mut self) -> IOResult<String> {
let mut str = String::new();
self.r.read_to_string(&mut str)?;
Ok(str)
}

fn parse_until<T>(&mut self, byte: u8, trim: &[char]) -> IOResult<T> where T: std::str::FromStr, <T as std::str::FromStr>::Err: std::error::Error + 'static {
let mut buf = Vec::new();
self.r.read_until(byte, &mut buf)?;
let str = String::from_utf8(buf)?;
Ok(str.trim_end_matches(trim).parse()?)
}

fn ln<T>(&mut self) -> IOResult<T> where T: std::str::FromStr, <T as std::str::FromStr>::Err: std::error::Error + 'static {
self.parse_until(0xA, &[0xD as char, 0xA as char]) // Trim both \r and \n on Windows
}

fn sp<T>(&mut self) -> IOResult<T> where T: std::str::FromStr, <T as std::str::FromStr>::Err: std::error::Error + 'static {
self.parse_until(0x20, &[0x20 as char])
}
}

#[allow(dead_code)]
fn split_sp<T>(s: &str) -> IOResult<Vec<T>> where T: std::str::FromStr, <T as std::str::FromStr>::Err: std::error::Error + 'static {
let mut vec = Vec::new();
for part in s.split_whitespace() {
vec.push(part.parse()?);
}
Ok(vec)
}

// IO delegates all Write methods to self.w
impl<R: Read, W: Write> Write for IO<R, W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { self.w.write(buf) }
fn flush(&mut self) -> std::io::Result<()> { self.w.flush() }
}

// Tests

#[cfg(test)]
mod test {
use std::fs::{self, File};
use super::{IO, IOResult, read_eval_print};

#[test]
fn run_tests() -> IOResult<()> {
let mut t = 0;
loop {
t += 1;
let in_path = format!("tests/{}.in", t);
let out_path = format!("tests/{}.out", t);
match File::open(in_path) {
Ok(r) => {
println!("Sample {}", t);
let w: Vec<u8> = Vec::new();
let mut io = IO::new(r, w);
read_eval_print(&mut io)?;
let out_received_bytes = io.w.into_inner()?;
let out_received = std::str::from_utf8(&out_received_bytes)?;
let out_expected_bytes = fs::read(out_path)?;
let out_expected = std::str::from_utf8(&out_expected_bytes)?;
assert_eq!(out_expected, out_received, "Wrong answer");
}
Err(_) => break
}
}
assert!(t > 1, "No samples were found");
println!("Total run {} samples", t - 1);
Ok(())
}
}
6 changes: 6 additions & 0 deletions Timus/2069/tests/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
2 3
4
8
2
7
3
1 change: 1 addition & 0 deletions Timus/2069/tests/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
8 changes: 8 additions & 0 deletions Timus/2069/tests/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
4 3
12
4
12
3
21
5
16
1 change: 1 addition & 0 deletions Timus/2069/tests/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12

0 comments on commit c86ef1c

Please sign in to comment.