This commit is contained in:
arielherself 2024-01-02 16:44:40 +08:00
parent 434f3b0d3e
commit d5902e616d
11 changed files with 287 additions and 0 deletions

5
src/a_sibling_file.rs Normal file
View File

@ -0,0 +1,5 @@
pub mod sibling_mod {
pub fn sibling_fn() {
println!("Hello, sibling!");
}
}

37
src/bin/lc-1552.rs Normal file
View File

@ -0,0 +1,37 @@
use std::io::stdin;
struct Solution;
impl Solution {
pub fn max_distance(mut position: Vec<i32>, 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::<i32>().unwrap()).collect::<Vec<i32>>();
buf.clear();
stdin().read_line(&mut buf).ok();
let m = buf.trim().parse::<i32>().unwrap();
println!("{}", Solution::max_distance(position, m));
}

54
src/bin/lc-1599.rs Normal file
View File

@ -0,0 +1,54 @@
struct Solution;
impl Solution {
pub fn min_operations_max_profit(customers: Vec<i32>, 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<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
buf.clear();
std::io::stdin().read_line(&mut buf).ok();
let boarding_cost: i32 = buf.trim().parse::<i32>().unwrap();
buf.clear();
std::io::stdin().read_line(&mut buf).ok();
let running_cost: i32 = buf.trim().parse::<i32>().unwrap();
println!("{}", Solution::min_operations_max_profit(customers, boarding_cost, running_cost));
}

33
src/bin/lc-1760.rs Normal file
View File

@ -0,0 +1,33 @@
struct Solution;
impl Solution {
pub fn minimum_size(nums: Vec<i32>, 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<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
buf.clear();
std::io::stdin().read_line(&mut buf).ok();
let max_operations: i32 = buf.trim().parse::<i32>().unwrap();
println!("{}", Solution::minimum_size(nums, max_operations));
}

36
src/bin/lc-2226.rs Normal file
View File

@ -0,0 +1,36 @@
struct Solution;
impl Solution {
pub fn maximum_candies(candies: Vec<i32>, 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<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
buf.clear();
std::io::stdin().read_line(&mut buf).ok();
let k: i64 = buf.trim().parse::<i64>().unwrap();
println!("{}",Solution::maximum_candies(candies, k));
}

28
src/bin/lc-2439.rs Normal file
View File

@ -0,0 +1,28 @@
struct Solution;
impl Solution {
pub fn minimize_array_value(nums: Vec<i32>) -> 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<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
println!("{}", Solution::minimize_array_value(nums));
}

50
src/bin/lc-2513.rs Normal file
View File

@ -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::<i32>().unwrap();
buf.clear();
std::io::stdin().read_line(&mut buf).ok();
let divisor2: i32 = buf.trim().parse::<i32>().unwrap();
buf.clear();
std::io::stdin().read_line(&mut buf).ok();
let unique_cnt1: i32 = buf.trim().parse::<i32>().unwrap();
buf.clear();
std::io::stdin().read_line(&mut buf).ok();
let unique_cnt2: i32 = buf.trim().parse::<i32>().unwrap();
println!("{}", Solution::minimize_set(divisor1, divisor2, unique_cnt1, unique_cnt2));
}

32
src/bin/lc-757.rs Normal file
View File

@ -0,0 +1,32 @@
struct Solution;
impl Solution {
pub fn intersection_size_two(mut intervals: Vec<Vec<i32>>) -> 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() {
}

5
src/test_mod.rs Normal file
View File

@ -0,0 +1,5 @@
pub mod a_mod {
pub fn hello_world() {
println!("Hello, world!");
}
}

5
src/test_mods/a_crate.rs Normal file
View File

@ -0,0 +1,5 @@
pub mod another_mod {
pub fn another_fn() {
println!("Hello world again!");
}
}

2
src/test_mods/mod.rs Normal file
View File

@ -0,0 +1,2 @@
mod a_crate;
pub use self::a_crate::another_mod;