backup
This commit is contained in:
parent
d5902e616d
commit
653d1c1d30
|
@ -0,0 +1,26 @@
|
|||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn can_see_persons_count(heights: Vec<i32>) -> Vec<i32> {
|
||||
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<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
println!("{:?}", Solution::can_see_persons_count(heights));
|
||||
}
|
|
@ -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<Vec<i32>>, num_select: i32) -> i32 {
|
||||
let m = matrix.len();
|
||||
let n = matrix[0].len();
|
||||
// 枚举选择的列
|
||||
let mut res = 0;
|
||||
for choice in 0..1<<n {
|
||||
if choice.count_ones() != num_select {
|
||||
continue;
|
||||
}
|
||||
let mut cnt = 0;
|
||||
for i in 0..m {
|
||||
let mut f = true;
|
||||
for j in 0..n {
|
||||
if matrix[i][j] == 1 && bit(choice, j) == 0 {
|
||||
f = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if f {
|
||||
cnt += 1;
|
||||
}
|
||||
}
|
||||
res = res.max(cnt);
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ListNode {
|
||||
pub val: i32,
|
||||
pub next: Option<Box<ListNode>>
|
||||
}
|
||||
|
||||
impl ListNode {
|
||||
#[inline]
|
||||
fn new(val: i32) -> Self {
|
||||
ListNode {
|
||||
next: None,
|
||||
val
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn remove_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
|
||||
use std::collections::VecDeque;
|
||||
let mut st = VecDeque::<Box<ListNode>>::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() {
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn maximum_tastiness(mut price: Vec<i32>, 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<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: i32 = buf.trim().parse::<i32>().unwrap();
|
||||
println!("{}", Solution::maximum_tastiness(price, k));
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn max_power(stations: Vec<i32>, 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::<i64>() > 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<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let r: i32 = buf.trim().parse::<i32>().unwrap();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let k: i32 = buf.trim().parse::<i32>().unwrap();
|
||||
println!("{}", Solution::max_power(stations, r, k));
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn min_capability(nums: Vec<i32>, 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<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: i32 = buf.trim().parse::<i32>().unwrap();
|
||||
println!("{}", Solution::min_capability(nums, k));
|
||||
}
|
|
@ -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<i32>, 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<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: i32 = buf.trim().parse::<i32>().unwrap();
|
||||
println!("{}", Solution::make_sub_k_sum_equal(arr, k));
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn minimize_max(mut nums: Vec<i32>, 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<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let p: i32 = buf.trim().parse::<i32>().unwrap();
|
||||
println!("{}", Solution::minimize_max(nums, p));
|
||||
}
|
|
@ -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::<String>().unwrap();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let needle: String = buf.trim().parse::<String>().unwrap();
|
||||
println!("{}", Solution::str_str(haystack, needle));
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
struct Solution;
|
||||
|
||||
struct QU {
|
||||
pa: Vec<usize>
|
||||
}
|
||||
|
||||
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<Vec<i32>>) -> 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::<usize>().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<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
grid[i] = line;
|
||||
}
|
||||
println!("{}", Solution::maximum_safeness_factor(grid));
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn max_number_of_alloys(n: i32, k: i32, budget: i32, composition: Vec<Vec<i32>>, stock: Vec<i32>, cost: Vec<i32>) -> 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() {
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
struct Solution;
|
||||
|
||||
struct QU(Vec<usize>);
|
||||
|
||||
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<Vec<i32>>) -> 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::<usize>().unwrap();
|
||||
let mut grid = vec![];
|
||||
for i in 0..n {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let line: Vec<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
grid[i] = line;
|
||||
}
|
||||
println!("{}", Solution::swim_in_water(grid));
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
struct Solution {
|
||||
n: usize,
|
||||
ch:Vec<Vec<usize>>,
|
||||
stat: HashMap<u64, u32>
|
||||
}
|
||||
|
||||
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<<self.n)-1))) {
|
||||
o + switch.count_ones()
|
||||
} else {
|
||||
u32::MAX
|
||||
};
|
||||
}
|
||||
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.dfs2(curr + 1, switch, status).min(self.dfs2(curr + 1, switch1, status1))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let nm: Vec<usize> = buf.trim().split(' ').map(|x|x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
|
||||
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<usize> = buf.trim().split(' ').map(|x|x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
|
||||
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::<u64, u32>::new()};
|
||||
solution.dfs1(0, 0, 0);
|
||||
println!("{}", solution.dfs2(n + 1 >> 1, 0, 0));
|
||||
}
|
|
@ -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<<SET_P],
|
||||
cnt: i32,
|
||||
tot: i32
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Addr {
|
||||
ct: i32,
|
||||
ci: i32,
|
||||
co: i32
|
||||
}
|
||||
|
||||
fn get_addr(i: i32, j: i32, src: bool) -> 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<<SET_P], cnt: 0, tot: 0 };
|
||||
let deb = 85;
|
||||
// for j in 0..M>>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);
|
||||
}
|
Loading…
Reference in New Issue