-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
60 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
2 3 | ||
4 | ||
8 | ||
2 | ||
7 | ||
3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
4 3 | ||
12 | ||
4 | ||
12 | ||
3 | ||
21 | ||
5 | ||
16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
12 |