From d5902e616dbd7a012b24fdbdcd0964e27f7ccd53 Mon Sep 17 00:00:00 2001 From: arielherself Date: Tue, 2 Jan 2024 16:44:40 +0800 Subject: [PATCH] backup --- src/a_sibling_file.rs | 5 ++++ src/bin/lc-1552.rs | 37 +++++++++++++++++++++++++++ src/bin/lc-1599.rs | 54 ++++++++++++++++++++++++++++++++++++++++ src/bin/lc-1760.rs | 33 ++++++++++++++++++++++++ src/bin/lc-2226.rs | 36 +++++++++++++++++++++++++++ src/bin/lc-2439.rs | 28 +++++++++++++++++++++ src/bin/lc-2513.rs | 50 +++++++++++++++++++++++++++++++++++++ src/bin/lc-757.rs | 32 ++++++++++++++++++++++++ src/test_mod.rs | 5 ++++ src/test_mods/a_crate.rs | 5 ++++ src/test_mods/mod.rs | 2 ++ 11 files changed, 287 insertions(+) create mode 100644 src/a_sibling_file.rs create mode 100644 src/bin/lc-1552.rs create mode 100644 src/bin/lc-1599.rs create mode 100644 src/bin/lc-1760.rs create mode 100644 src/bin/lc-2226.rs create mode 100644 src/bin/lc-2439.rs create mode 100644 src/bin/lc-2513.rs create mode 100644 src/bin/lc-757.rs create mode 100644 src/test_mod.rs create mode 100644 src/test_mods/a_crate.rs create mode 100644 src/test_mods/mod.rs diff --git a/src/a_sibling_file.rs b/src/a_sibling_file.rs new file mode 100644 index 0000000..b66bb7b --- /dev/null +++ b/src/a_sibling_file.rs @@ -0,0 +1,5 @@ +pub mod sibling_mod { + pub fn sibling_fn() { + println!("Hello, sibling!"); + } +} \ No newline at end of file diff --git a/src/bin/lc-1552.rs b/src/bin/lc-1552.rs new file mode 100644 index 0000000..2f9a894 --- /dev/null +++ b/src/bin/lc-1552.rs @@ -0,0 +1,37 @@ +use std::io::stdin; + +struct Solution; + +impl Solution { + pub fn max_distance(mut position: Vec, m: i32) -> i32 { + position.sort_unstable(); + let mut l = 0; + let mut r = position.last().unwrap().to_owned() - position.first().unwrap().to_owned(); + while l != r { + let mid = l + r + 1 >> 1; + let mut prev = -mid; + let mut cnt = 0; + for &x in position.iter() { + if x - prev < mid { continue; } + cnt += 1; + prev = x; + } + if cnt < m { + r = mid - 1; + } else { + l = mid; + } + } + l + } +} + +fn main() { + let mut buf = String::new(); + stdin().read_line(&mut buf).ok(); + let position = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + buf.clear(); + stdin().read_line(&mut buf).ok(); + let m = buf.trim().parse::().unwrap(); + println!("{}", Solution::max_distance(position, m)); +} \ No newline at end of file diff --git a/src/bin/lc-1599.rs b/src/bin/lc-1599.rs new file mode 100644 index 0000000..5cc1d24 --- /dev/null +++ b/src/bin/lc-1599.rs @@ -0,0 +1,54 @@ +struct Solution; + +impl Solution { + pub fn min_operations_max_profit(customers: Vec, boarding_cost: i32, running_cost: i32) -> i32 { + let delta = 4 * boarding_cost - running_cost; + let mut max_tm = 0; + let mut max_amount = 0; + let mut curr_tm = 0; + let mut curr_amount = 0; + let mut cand_amount = 0; + let mut ac = 0; + for (i, &x) in customers.iter().enumerate() { + let i = i as i32; + if curr_tm < i { + ac = 0; + curr_tm = i; + curr_amount = cand_amount - (i - curr_tm) * running_cost; + } + let x = x + ac; + curr_amount += (x >> 2) * delta; + curr_tm += x >> 2; + if curr_amount > max_amount || curr_amount == max_amount && curr_tm < max_tm { + max_tm = curr_tm; + max_amount = curr_amount; + } + let residual = x - (x >> 2 << 2); + ac = residual; + let tmp = curr_amount + residual * boarding_cost - running_cost; + cand_amount = tmp; + if tmp > max_amount { + max_tm = curr_tm + 1; + max_amount = tmp; + } + } + if max_amount <= 0 { + -1 + } else { + max_tm + } + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let customers: Vec = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let boarding_cost: i32 = buf.trim().parse::().unwrap(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let running_cost: i32 = buf.trim().parse::().unwrap(); + println!("{}", Solution::min_operations_max_profit(customers, boarding_cost, running_cost)); +} \ No newline at end of file diff --git a/src/bin/lc-1760.rs b/src/bin/lc-1760.rs new file mode 100644 index 0000000..307bcd1 --- /dev/null +++ b/src/bin/lc-1760.rs @@ -0,0 +1,33 @@ +struct Solution; + +impl Solution { + pub fn minimum_size(nums: Vec, max_operations: i32) -> i32 { + let mut l = 1; + let mut r = *nums.iter().max().unwrap(); + while l != r { + let mid = l + r >> 1; + eprint!("{:?} ", mid); + let mut cnt = 0; + for &x in nums.iter() { + cnt += (x - 1) / mid; + } + eprintln!("{:?}", cnt); + if cnt > max_operations { + l = mid + 1; + } else { + r = mid; + } + } + l + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let nums: Vec = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let max_operations: i32 = buf.trim().parse::().unwrap(); + println!("{}", Solution::minimum_size(nums, max_operations)); +} \ No newline at end of file diff --git a/src/bin/lc-2226.rs b/src/bin/lc-2226.rs new file mode 100644 index 0000000..ec3bef8 --- /dev/null +++ b/src/bin/lc-2226.rs @@ -0,0 +1,36 @@ +struct Solution; + +impl Solution { + pub fn maximum_candies(candies: Vec, k: i64) -> i32 { + let check = |m: i32| { + let mut cnt = 0; + candies.iter().for_each(|&x|cnt += (x / m) as i64); + cnt >= k + }; + let mut l = 1; + let mut r = *candies.iter().min().unwrap() ; + while l != r { + let mid = l + r + 1 >> 1; + if !check(mid) { + r = mid - 1; + } else { + l = mid; + } + } + if check(l) { + l + } else { + 0 + } + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let candies: Vec = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let k: i64 = buf.trim().parse::().unwrap(); + println!("{}",Solution::maximum_candies(candies, k)); +} \ No newline at end of file diff --git a/src/bin/lc-2439.rs b/src/bin/lc-2439.rs new file mode 100644 index 0000000..27f9c8f --- /dev/null +++ b/src/bin/lc-2439.rs @@ -0,0 +1,28 @@ +struct Solution; + +impl Solution { + pub fn minimize_array_value(nums: Vec) -> i32 { + let mut l = 0; + let mut r = *nums.iter().max().unwrap(); + while l != r { + let mid = l + r >> 1; + let mut ac = 0i64; + for &x in nums.iter().rev() { + ac = 0.max(x as i64 + ac - mid as i64); + } + if ac > 0 { + l = mid + 1; + } else { + r = mid; + } + } + l + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let nums: Vec = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + println!("{}", Solution::minimize_array_value(nums)); +} \ No newline at end of file diff --git a/src/bin/lc-2513.rs b/src/bin/lc-2513.rs new file mode 100644 index 0000000..798e605 --- /dev/null +++ b/src/bin/lc-2513.rs @@ -0,0 +1,50 @@ +struct Solution; + +impl Solution { + pub fn minimize_set(divisor1: i32, divisor2: i32, unique_cnt1: i32, unique_cnt2: i32) -> i32 { + let divisor1 = divisor1 as i64; + let divisor2 = divisor2 as i64; + let unique_cnt1 = unique_cnt1 as i64; + let unique_cnt2 = unique_cnt2 as i64; + let gcd = |mut x: i64, mut y: i64|{ + while y != 0 { + (x, y) = (y, x % y); + } + x + }; + let mut l = 1; + let mut r = i64::MAX; + let lcm = divisor1 * divisor2 / gcd(divisor1, divisor2); + while l != r { + let mid = l + (r - l >> 1); // mid == 11 + let a = mid / divisor1; // a == 5 + let b = mid / divisor2; // b == 2, c == 2 + let c = mid / lcm; // c == 3 + let o = mid - a - b + c; // o == 6 + eprintln!("mid = {mid}, a = {a}, b = {b}, c = {c}, o = {o}"); + if b - c + o < unique_cnt1 || a - c + o - 0.max(unique_cnt1 - b + c) < unique_cnt2 { + l = mid + 1; + } else { + r = mid; + } + } + l as i32 + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let divisor1: i32 = buf.trim().parse::().unwrap(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let divisor2: i32 = buf.trim().parse::().unwrap(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let unique_cnt1: i32 = buf.trim().parse::().unwrap(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let unique_cnt2: i32 = buf.trim().parse::().unwrap(); + println!("{}", Solution::minimize_set(divisor1, divisor2, unique_cnt1, unique_cnt2)); + +} \ No newline at end of file diff --git a/src/bin/lc-757.rs b/src/bin/lc-757.rs new file mode 100644 index 0000000..037e3ca --- /dev/null +++ b/src/bin/lc-757.rs @@ -0,0 +1,32 @@ +struct Solution; + +impl Solution { + pub fn intersection_size_two(mut intervals: Vec>) -> i32 { + intervals.sort_unstable_by(|x,y| + if x[1] == y[1] { + y[0].cmp(&x[0]) + } else { + x[1].cmp(&y[1]) + } + ); + let mut t1 = -1; + let mut t2 = -1; + let mut res = 0; + for x in intervals { + if x[0] > t2 { + t1 = x[1] - 1; + t2 = x[1]; + res += 2; + } else if x[0] > t1 { + t1 = t2; + t2 = x[1]; + res += 1 + } + } + res + } +} + +fn main() { + +} \ No newline at end of file diff --git a/src/test_mod.rs b/src/test_mod.rs new file mode 100644 index 0000000..511d0e1 --- /dev/null +++ b/src/test_mod.rs @@ -0,0 +1,5 @@ +pub mod a_mod { + pub fn hello_world() { + println!("Hello, world!"); + } +} \ No newline at end of file diff --git a/src/test_mods/a_crate.rs b/src/test_mods/a_crate.rs new file mode 100644 index 0000000..e27c0c1 --- /dev/null +++ b/src/test_mods/a_crate.rs @@ -0,0 +1,5 @@ +pub mod another_mod { + pub fn another_fn() { + println!("Hello world again!"); + } +} \ No newline at end of file diff --git a/src/test_mods/mod.rs b/src/test_mods/mod.rs new file mode 100644 index 0000000..da4b606 --- /dev/null +++ b/src/test_mods/mod.rs @@ -0,0 +1,2 @@ +mod a_crate; +pub use self::a_crate::another_mod;