-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lance-Drane <ldraneutk@gmail.com>
- Loading branch information
1 parent
312eff5
commit 8a589ce
Showing
1 changed file
with
107 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,107 @@ | ||
// I/O boilerplate // | ||
|
||
pub struct UnsafeScanner<'a> { | ||
// not actually dead code, needed for buf_iter to work | ||
#[allow(dead_code)] | ||
buf_str: Vec<u8>, | ||
buf_iter: std::str::SplitAsciiWhitespace<'a>, | ||
} | ||
|
||
impl UnsafeScanner<'_> { | ||
pub fn new<R: std::io::Read>(mut reader: R) -> Self { | ||
let mut buf_str = vec![]; | ||
unsafe { | ||
reader.read_to_end(&mut buf_str).unwrap_unchecked(); | ||
} | ||
let buf_iter = unsafe { | ||
let slice = std::str::from_utf8_unchecked(&buf_str); | ||
std::mem::transmute(slice.split_ascii_whitespace()) | ||
}; | ||
|
||
Self { buf_str, buf_iter } | ||
} | ||
|
||
/// Use "turbofish" syntax `token::<T>()` to select data type of next token. | ||
/// | ||
/// # Panics | ||
/// Panics if there's no more tokens or if the token cannot be parsed as T. | ||
pub fn token<T: std::str::FromStr>(&mut self) -> T { | ||
unsafe { | ||
self.buf_iter | ||
.next() | ||
.unwrap_unchecked() | ||
.parse() | ||
.unwrap_unchecked() | ||
} | ||
} | ||
} | ||
|
||
// problem // | ||
|
||
/// You have n coins with positive integer values. What is the smallest sum you cannot create using a subset of the coins? | ||
/// | ||
/// <b>Input</b> | ||
/// | ||
/// The first input line has an integer n: the number of coins. | ||
/// | ||
/// The second line has n integers x<sub>1</sub>,x<sub>2</sub>,...,x<sub>n</sub>: the value of each coin. | ||
/// | ||
/// <b>Output</b> | ||
/// | ||
/// Print one integer: the smallest coin sum.. | ||
/// | ||
/// <b>Constraints</b> | ||
/// | ||
/// <ul> | ||
/// <li>1 ≤ n ≤ 2 * 10<sup>5</sup></li> | ||
/// <li>1 ≤ x<sub>i</sub> ≤ 10<sup>9</sup></li> | ||
/// </ul> | ||
fn solve<W: std::io::Write>(mut scan: UnsafeScanner, out: &mut W) { | ||
let n: u32 = scan.token(); | ||
let mut coins: Vec<u64> = (0..n).map(|_| scan.token()).collect(); | ||
coins.sort_unstable(); | ||
|
||
let mut sum = 1; | ||
|
||
for coin in coins { | ||
if coin > sum { | ||
break; | ||
} | ||
sum += coin; | ||
} | ||
writeln!(out, "{sum}").ok(); | ||
} | ||
|
||
// entrypoints // | ||
|
||
fn main() { | ||
let scan = UnsafeScanner::new(std::io::stdin()); | ||
let mut out = std::io::BufWriter::new(std::io::stdout().lock()); | ||
solve(scan, &mut out); | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
fn test(input: &[u8], target: &[u8]) { | ||
let scan = UnsafeScanner::new(input); | ||
let mut out = Vec::with_capacity(target.len()); | ||
solve(scan, &mut out); | ||
|
||
assert_eq!(out, target); | ||
} | ||
|
||
#[test] | ||
fn test_example() { | ||
let input = b"\ | ||
5 | ||
2 9 1 2 7 | ||
"; | ||
let target = b"\ | ||
6 | ||
"; | ||
|
||
test(input, target); | ||
} | ||
} |