Datasets:
Tasks:
Text Generation
Modalities:
Text
Formats:
json
Languages:
English
Size:
< 1K
ArXiv:
Tags:
competitive-programming
code-generation
specification-generation
proof-generation
verification
rust
License:
File size: 1,877 Bytes
e5529fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | impl Solution {
fn count_above_exec(inventory: &Vec<i32>, threshold: i64) -> i64 {
let n = inventory.len();
let mut count: i64 = 0;
let mut j: usize = 0;
while j < n {
if inventory[j] as i64 > threshold {
count += inventory[j] as i64 - threshold;
}
j += 1;
}
count
}
pub fn max_profit(inventory: Vec<i32>, orders: i32) -> i32 {
let n = inventory.len();
let orders_i64 = orders as i64;
let modulo: i128 = 1_000_000_007;
let mut max_inv: i64 = 0;
let mut i: usize = 0;
while i < n {
if inventory[i] as i64 > max_inv {
max_inv = inventory[i] as i64;
}
i += 1;
}
let mut lo: i64 = 0;
let mut hi: i64 = max_inv;
while lo < hi {
let mid = lo + (hi - lo) / 2;
let count = Self::count_above_exec(&inventory, mid);
if count <= orders_i64 {
hi = mid;
} else {
lo = mid + 1;
}
}
let threshold = lo;
let mut total: i128 = 0;
let mut sold: i64 = 0;
let mut k: usize = 0;
while k < n {
let inv = inventory[k] as i64;
if inv > threshold {
sold += inv - threshold;
let inv128 = inv as i128;
let thr128 = threshold as i128;
let a = inv128 + thr128 + 1;
let b = inv128 - thr128;
let contrib = (inv128 + thr128 + 1) * (inv128 - thr128) / 2;
total += contrib;
}
k += 1;
}
let rem128 = (orders_i64 - sold) as i128;
let thr_rem = threshold as i128;
total += rem128 * thr_rem;
(total % modulo) as i32
}
}
|