From 653d1c1d30635631e739f18d21a272388317a690 Mon Sep 17 00:00:00 2001 From: arielherself Date: Fri, 5 Jan 2024 12:56:21 +0800 Subject: [PATCH] backup --- src/bin/lc-1944.rs | 26 ++++++++++ src/bin/lc-2397.rs | 40 ++++++++++++++++ src/bin/lc-2487.rs | 46 ++++++++++++++++++ src/bin/lc-2517.rs | 36 ++++++++++++++ src/bin/lc-2528.rs | 51 ++++++++++++++++++++ src/bin/lc-2560.rs | 34 +++++++++++++ src/bin/lc-2607.rs | 40 ++++++++++++++++ src/bin/lc-2616.rs | 41 ++++++++++++++++ src/bin/lc-28.rs | 47 ++++++++++++++++++ src/bin/lc-2812.rs | 97 ++++++++++++++++++++++++++++++++++++++ src/bin/lc-2861.rs | 34 +++++++++++++ src/bin/lc-778.rs | 60 +++++++++++++++++++++++ src/bin/lg-2962.rs | 67 ++++++++++++++++++++++++++ src/bin/test_cache_miss.rs | 91 +++++++++++++++++++++++++++++++++++ 14 files changed, 710 insertions(+) create mode 100644 src/bin/lc-1944.rs create mode 100644 src/bin/lc-2397.rs create mode 100644 src/bin/lc-2487.rs create mode 100644 src/bin/lc-2517.rs create mode 100644 src/bin/lc-2528.rs create mode 100644 src/bin/lc-2560.rs create mode 100644 src/bin/lc-2607.rs create mode 100644 src/bin/lc-2616.rs create mode 100644 src/bin/lc-28.rs create mode 100644 src/bin/lc-2812.rs create mode 100644 src/bin/lc-2861.rs create mode 100644 src/bin/lc-778.rs create mode 100644 src/bin/lg-2962.rs create mode 100644 src/bin/test_cache_miss.rs diff --git a/src/bin/lc-1944.rs b/src/bin/lc-1944.rs new file mode 100644 index 0000000..275d9db --- /dev/null +++ b/src/bin/lc-1944.rs @@ -0,0 +1,26 @@ +struct Solution; + +impl Solution { + pub fn can_see_persons_count(heights: Vec) -> Vec { + let mut st = vec![]; + let mut res = vec![]; + for &x in heights.iter().rev() { + let mut cnt = 0; + while let Some(&p) = st.last() { + cnt += 1; + if p > x { break; } + st.pop(); + } + st.push(x); + res.push(cnt); + } + res.into_iter().rev().collect() + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let heights: Vec = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + println!("{:?}", Solution::can_see_persons_count(heights)); +} \ No newline at end of file diff --git a/src/bin/lc-2397.rs b/src/bin/lc-2397.rs new file mode 100644 index 0000000..22fb043 --- /dev/null +++ b/src/bin/lc-2397.rs @@ -0,0 +1,40 @@ +struct Solution; + +#[inline] +fn bit(x: i32, b: usize) -> i32 { + (x >> b) & 1 +} + +impl Solution { + pub fn maximum_rows(matrix: Vec>, num_select: i32) -> i32 { + let m = matrix.len(); + let n = matrix[0].len(); + // 枚举选择的列 + let mut res = 0; + for choice in 0..1<> +} + +impl ListNode { + #[inline] + fn new(val: i32) -> Self { + ListNode { + next: None, + val + } + } +} + +struct Solution; + +impl Solution { + pub fn remove_nodes(head: Option>) -> Option> { + use std::collections::VecDeque; + let mut st = VecDeque::>::new(); + let mut curr = head; + while let Some(mut node) = curr { + while matches!(st.back(), Some(prev) if prev.val < node.val) { + st.pop_back(); + } + while let Some(prev) = st.back() { + if prev.val >= node.val { break; } + st.pop_back(); + } + curr = node.next.take(); + st.push_back(node); + } + let mut next = st.pop_back().unwrap(); + while let Some(mut node) = st.pop_back() { + node.next = Some(next); + next = node; + } + Some(next) + } +} + +fn main() { + +} \ No newline at end of file diff --git a/src/bin/lc-2517.rs b/src/bin/lc-2517.rs new file mode 100644 index 0000000..d89bf94 --- /dev/null +++ b/src/bin/lc-2517.rs @@ -0,0 +1,36 @@ +struct Solution; + +impl Solution { + pub fn maximum_tastiness(mut price: Vec, k: i32) -> i32 { + price.sort_unstable(); + let mut l = 0; + let mut r = price.last().unwrap() - price.first().unwrap(); + while l < r { + let mid = l + r + 1 >> 1; + let mut fm = -mid; + let mut cnt = 0; + for &x in price.iter() { + if x - fm >= mid { + cnt += 1; + fm = x; + } + } + if cnt < k { + r = mid - 1; + } else { + l = mid; + } + } + l + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let price: Vec = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let k: i32 = buf.trim().parse::().unwrap(); + println!("{}", Solution::maximum_tastiness(price, k)); +} \ No newline at end of file diff --git a/src/bin/lc-2528.rs b/src/bin/lc-2528.rs new file mode 100644 index 0000000..f524bd7 --- /dev/null +++ b/src/bin/lc-2528.rs @@ -0,0 +1,51 @@ +struct Solution; + +impl Solution { + pub fn max_power(stations: Vec, r: i32, k: i32) -> i64 { + let n = stations.len(); + let r = r as usize; + let mut left = 0; + let mut right = stations.iter().fold(0i64, |s, &x|s+x as i64) + k as i64; + while left < right{ + let mid = left + right + 1 >> 1; + let mut cnt = 0i64; + let mut extra = vec![0;n]; + for i in 0..r { + cnt += stations[i] as i64; + } + for i in 0..n { + // eprintln!("mid = {mid}, cnt = {cnt}"); + if i + r < n { + cnt += stations[i+r] as i64 + extra[i+r]; + } + if i > r { + cnt -= stations[i-r-1] as i64 + extra[i-r-1]; + } + if cnt < mid { + let pos = (i + r).min(n - 1); + extra[pos] += mid - cnt; + cnt += mid - cnt; + } + } + if extra.iter().sum::() > k as i64 { + right = mid - 1; + } else { + left = mid; + } + } + left + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let stations: Vec = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let r: i32 = buf.trim().parse::().unwrap(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let k: i32 = buf.trim().parse::().unwrap(); + println!("{}", Solution::max_power(stations, r, k)); +} \ No newline at end of file diff --git a/src/bin/lc-2560.rs b/src/bin/lc-2560.rs new file mode 100644 index 0000000..a38a7c6 --- /dev/null +++ b/src/bin/lc-2560.rs @@ -0,0 +1,34 @@ +struct Solution; + +impl Solution { + pub fn min_capability(nums: Vec, k: i32) -> i32 { + let mut l = 0; + let mut r = *nums.iter().max().unwrap(); + while l < r { + let mid = l + r >> 1; + let mut cnt1 = 0; + let mut cnt2 = 0; + for &x in &nums { + let new = cnt2.max(cnt1 + if x <= mid {1} else {0}); + cnt1 = cnt2; + cnt2 = new; + } + if cnt1.max(cnt2) < k { + 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 k: i32 = buf.trim().parse::().unwrap(); + println!("{}", Solution::min_capability(nums, k)); +} \ No newline at end of file diff --git a/src/bin/lc-2607.rs b/src/bin/lc-2607.rs new file mode 100644 index 0000000..b957e61 --- /dev/null +++ b/src/bin/lc-2607.rs @@ -0,0 +1,40 @@ +// 每个元素都是模k同余类的中位数 +// a b c d a d c + +struct Solution; + +impl Solution { + pub fn make_sub_k_sum_equal(arr: Vec, k: i32) -> i64 { + let gcd = |mut x: usize, mut y: usize| { + while y != 0 { + (x, y) = (y, x % y); + } + x + }; + let n = arr.len(); + let k = gcd(n, k as usize); + let mut res = 0i64; + let mut grp = vec![vec![];k]; + for (i,x) in arr.into_iter().enumerate() { + let id = i.rem_euclid(k); + grp[id].push(x); + } + for mut l in grp { + l.sort_unstable(); + let len = l.len(); + let curr = l.iter().skip(len>>1).fold(0i64, |s,&x|s+x as i64) - l.iter().take(len+1>>1).fold(0i64, |s,&x|s+x as i64); + res += curr; + } + res + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let arr: Vec = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let k: i32 = buf.trim().parse::().unwrap(); + println!("{}", Solution::make_sub_k_sum_equal(arr, k)); +} \ No newline at end of file diff --git a/src/bin/lc-2616.rs b/src/bin/lc-2616.rs new file mode 100644 index 0000000..34f5d9d --- /dev/null +++ b/src/bin/lc-2616.rs @@ -0,0 +1,41 @@ +struct Solution; + +impl Solution { + pub fn minimize_max(mut nums: Vec, p: i32) -> i32 { + let n = nums.len(); + nums.sort_unstable(); + let mut diff = vec![0;n-1]; + for i in 1..n { + diff[i-1] = nums[i] - nums[i-1]; + } + // eprintln!("{:?}", diff); + let mut l = 0; + let mut r = *nums.iter().max().unwrap() - *nums.iter().min().unwrap(); + while l < r { + let mid = l + r >> 1; + let mut dp1 = 0; + let mut dp2 = 0; + for &x in &diff { + let new = dp2.max(dp1 + if x <= mid {1} else {0}); + dp1 = dp2; + dp2 = new; + } + if dp2 < p { + 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 p: i32 = buf.trim().parse::().unwrap(); + println!("{}", Solution::minimize_max(nums, p)); +} \ No newline at end of file diff --git a/src/bin/lc-28.rs b/src/bin/lc-28.rs new file mode 100644 index 0000000..0090136 --- /dev/null +++ b/src/bin/lc-28.rs @@ -0,0 +1,47 @@ +struct Solution; + +impl Solution { + pub fn str_str(haystack: String, needle: String) -> i32 { + let n = haystack.len(); + let m = needle.len(); + let haystack = haystack.into_bytes(); + let needle = needle.into_bytes(); + let mut next = vec![-1;m]; + let mut j = -1; + let mut i = 0; + while i + 1 < m as i32 { + if j == -1 || needle[i as usize] == needle[j as usize] { + i += 1; + j += 1; + next[i as usize] = j; + } else { + j = next[j as usize]; + } + } + eprintln!("{:?}", next); + i = 0; // haystack + j = 0; // needle + while i < n as i32 { + if j == -1 || haystack[i as usize] == needle[j as usize] { + i += 1; + j += 1; + } else { + j = next[j as usize]; + } + if j == m as i32 { + return i - m as i32; + } + } + -1 + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let haystack: String = buf.trim().parse::().unwrap(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let needle: String = buf.trim().parse::().unwrap(); + println!("{}", Solution::str_str(haystack, needle)); +} \ No newline at end of file diff --git a/src/bin/lc-2812.rs b/src/bin/lc-2812.rs new file mode 100644 index 0000000..9a827ac --- /dev/null +++ b/src/bin/lc-2812.rs @@ -0,0 +1,97 @@ +struct Solution; + +struct QU { + pa: Vec +} + +impl QU { + fn query(&mut self, i: usize) -> usize { + if self.pa[i] != i { + self.pa[i] = self.query(self.pa[i]); + } + self.pa[i] + } + fn merge(&mut self, i: usize, j: usize) { + let qi = self.query(i); + let qj = self.query(j); + self.pa[qi] = qj; + } +} + +impl Solution { + pub fn maximum_safeness_factor(grid: Vec>) -> i32 { + use std::collections::VecDeque; + let n = grid.len(); + let mut dis = vec![vec![0;n];n]; + let mut dq = VecDeque::new(); + for i in 0..n { + for j in 0..n { + if grid[i][j] == 1 { + dq.push_back([0,i,j]); + } + } + } + // 0-1 bfs + let mut vis = vec![vec![false;n];n]; + while let Some([d,i,j]) = dq.pop_front() { + if vis[i][j] { continue; } + vis[i][j] = true; + dis[i][j] = d; + if j + 1 != n { + dq.push_back([d+1,i,j+1]); + } + if j != 0 { + dq.push_back([d+1,i,j-1]); + } + if i + 1 != n { + dq.push_back([d+1,i+1,j]); + } + if i != 0 { + dq.push_back([d+1,i-1,j]); + } + } + // binary search + let mut l = 0; + let mut r = n << 1; + let n2 = n * n; + while l != r { + let mid = l + r + 1 >> 1; + let mut qu = QU{pa: (0..n2).collect()}; + for i in 0..n { + for j in 0..n { + if dis[i][j] < mid { continue; } + if i + 1 != n && dis[i+1][j] >= mid { + qu.merge(i*n+j, (i+1)*n+j); + } + if j + 1 != n && dis[i][j+1] >= mid { + qu.merge(i*n+j, i*n+j+1); + } + } + } + if qu.query(0) != qu.query(n*n-1) { + r = mid - 1; + } else { + l = mid; + } + } + if l == n << 1 { + 0 + } else { + l as i32 + } + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let n: usize = buf.trim().parse::().unwrap(); + let mut grid = vec![vec![];n]; + for i in 0..n { + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let line: Vec = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + grid[i] = line; + } + println!("{}", Solution::maximum_safeness_factor(grid)); +} \ No newline at end of file diff --git a/src/bin/lc-2861.rs b/src/bin/lc-2861.rs new file mode 100644 index 0000000..683b720 --- /dev/null +++ b/src/bin/lc-2861.rs @@ -0,0 +1,34 @@ +struct Solution; + +impl Solution { + pub fn max_number_of_alloys(n: i32, k: i32, budget: i32, composition: Vec>, stock: Vec, cost: Vec) -> i32 { + let budget = budget as i64; + let mut l = 0; + let mut r = i32::MAX as i64; + let types = stock.len(); + while l < r { + let mid = l + (r - l + 1 >> 1); + let mut viable = false; + for line in &composition { + let mut tot_cost = 0; + for i in 0..types { + tot_cost += 0.max(mid * line[i] as i64 - stock[i] as i64) * cost[i] as i64; + } + if tot_cost <= budget { + viable = true; + break; + } + } + if !viable { + r = mid - 1; + } else { + l = mid; + } + } + l as i32 + } +} + +fn main() { + +} \ No newline at end of file diff --git a/src/bin/lc-778.rs b/src/bin/lc-778.rs new file mode 100644 index 0000000..e21a6d7 --- /dev/null +++ b/src/bin/lc-778.rs @@ -0,0 +1,60 @@ +struct Solution; + +struct QU(Vec); + +impl QU { + fn query(&mut self, i: usize) -> usize { + if self.0[i] != i { + self.0[i] = self.query(self.0[i]); + } + self.0[i] + } + fn merge(&mut self, i: usize, j: usize) { + let t = self.query(i); + self.0[t] = self.query(j); + } +} + +impl Solution { + pub fn swim_in_water(grid: Vec>) -> i32 { + let n = grid.len(); + let n2 = n * n; + let mut l = 0; + let mut r = n2 as i32; + while l < r { + let mid = l + r >> 1; + let mut qu = QU((0..n2).collect()); + for i in 0..n { + for j in 0..n { + if grid[i][j] > mid { continue; } + if i + 1 != n && grid[i+1][j] <= mid { + qu.merge(i*n+j, (i+1)*n+j); + } + if j + 1 != n && grid[i][j+1] <= mid { + qu.merge(i*n+j, i*n+j+1); + } + } + } + if qu.query(0) != qu.query(n2-1) { + l = mid + 1; + } else { + r = mid; + } + } + l + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let n: usize = buf.trim().parse::().unwrap(); + let mut grid = vec![]; + for i in 0..n { + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let line: Vec = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + grid[i] = line; + } + println!("{}", Solution::swim_in_water(grid)); +} \ No newline at end of file diff --git a/src/bin/lg-2962.rs b/src/bin/lg-2962.rs new file mode 100644 index 0000000..9978e38 --- /dev/null +++ b/src/bin/lg-2962.rs @@ -0,0 +1,67 @@ +use std::collections::HashMap; + +struct Solution { + n: usize, + ch:Vec>, + stat: HashMap +} + +impl Solution { + fn dfs1(&mut self, curr: usize, switch: u64, status: u64) { + if curr == self.n + 1 >> 1 { + if let Some(&pr) = self.stat.get(&status) { + self.stat.insert(status, switch.count_ones().min(pr)); + } else { + self.stat.insert(status, switch.count_ones()); + } + return; + } + let ch_cnt = self.ch[curr].len(); + let bit = 1 << curr; + let switch1 = switch ^ bit; + let mut status1 = status ^ bit; + for i in 0..ch_cnt { + let child = self.ch[curr][i]; + status1 ^= 1 << child; + } + self.dfs1(curr + 1, switch, status); + self.dfs1(curr + 1, switch1, status1); + } + fn dfs2(&mut self, curr: usize, switch: u64, status: u64) -> u32 { + if curr == self.n { + return if let Some(&o) = self.stat.get(&(!status&((1< = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + let (n, m) = (nm[0], nm[1]); + let mut ch = vec![vec![];n]; + for _ in 0..m { + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let uv: Vec = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + let (u, v) = (uv[0], uv[1]); + ch[u-1].push(v-1); + ch[v-1].push(u-1); + } + let mut solution = Solution{n, ch, stat: HashMap::::new()}; + solution.dfs1(0, 0, 0); + println!("{}", solution.dfs2(n + 1 >> 1, 0, 0)); +} \ No newline at end of file diff --git a/src/bin/test_cache_miss.rs b/src/bin/test_cache_miss.rs new file mode 100644 index 0000000..5184070 --- /dev/null +++ b/src/bin/test_cache_miss.rs @@ -0,0 +1,91 @@ +const N: i32 = 17; +const M: i32 = 17; +const SET_P: usize = 0; +const OFF_P: usize = 4; +const SIZE_P: usize = 1; +const OFFSET: i32 = 0; + +struct Cache { + tags: [i32;1< Addr { + let bin = OFFSET + (if src {i * M + j} else {(N + i) * M + j} << SIZE_P); + Addr { + ct: bin >> (SET_P + OFF_P), + ci: (bin >> OFF_P) & ((1 << SET_P) - 1), + co: bin & ((1 << OFF_P) - 1) + } +} + +fn get_src_addr(i: i32, j: i32) -> Addr { + get_addr(i, j, true) +} + +fn get_dst_addr(i: i32, j: i32) -> Addr { + get_addr(i, j, false) +} + +impl Cache { + fn simulate(&mut self, addr: &Addr) -> bool { + self.tot += 1; + let tag = addr.ct; + let idx = addr.ci; + if tag != self.tags[idx as usize] { + self.tags[idx as usize] = tag; + self.cnt += 1; + false + } else { + true + } + } +} + +fn main() { + let mut cache = Cache{ tags: [-1;1<>1 { + // for i in 0..N>>1 { + // let i = i << 1; + // let j = j << 1; + // let mut r = [false;4]; + // r[0] = cache.simulate(&get_src_addr(i, j)); + // r[1] = cache.simulate(&get_src_addr(i+1, j)); + // r[2] = cache.simulate(&get_src_addr(i, j+1)); + // r[3] = cache.simulate(&get_src_addr(i+1, j+1)); + // if i == deb { + // println!("i = {i}, j = {j}, addr = {:?}, missed = {:?}", get_src_addr(i, j), r[0]); + // println!("i = {i}, j = {}, addr = {:?}, missed = {:?}", j+1, get_src_addr(i, j+1), r[2]); + // } + // } + // } + for j in 0..4 { + for i in 0..4 { + let addr = get_src_addr(i, j); + cache.simulate(&addr); + } + } + // for i in 0..N { + // for j in 0..M { + // let src_addr = get_src_addr(i, j); + // let dst_addr = get_dst_addr(i, j); + // let r1 = cache.simulate(&src_addr); + // let r2 = cache.simulate(&dst_addr); + // if i == deb { + // println!("i = {i}, j = {j}, addr = {:?}, missed = {:?}", src_addr, r1); + // } + // } + // } + let cnt = cache.cnt; + let tot = cache.tot; + println!("cnt = {cnt}, tot = {tot}, miss rate = {}", cnt as f64/tot as f64); +} \ No newline at end of file