not completed, still need to be fixes. Not fully working/tested

main
Luca Lombardo 1 year ago
parent 1ea44355a8
commit 31ba3abfe4

@ -1,26 +0,0 @@
fn main() {
// reads two integers n and k from the standard input.
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap(); // read line from stdin
let mut iter = input.split_whitespace(); // split input string by whitespace
let n: usize = iter.next().unwrap().parse().unwrap(); // parse first element to usize
let k: usize = iter.next().unwrap().parse().unwrap(); // parse second element to usize
// reads n integers from the standard input and stores them in an array a.
let mut input = String::new(); // input string
std::io::stdin().read_line(&mut input).unwrap(); // read line from stdin
let mut iter = input.split_whitespace(); // split input string by whitespace
let mut a: Vec<usize> = Vec::with_capacity(n); // create vector with capacity n
for _ in 0..n {
a.push(iter.next().unwrap().parse().unwrap()); // parse each element to usize and push to vector
}
let mut l = 0; // left index
let mut r = 0; // right index
let mut max = 0; // max length
let mut count = 0; // count of different elements
// We maintain a hashmap to store the count of each value in the current segment. We start with the left and right indices at 0 and keep incrementing the right index until the number of different values in the segment exceeds `k`. At this point, we increment the left index until the number of different values in the segment is less than or equal to `k`. We keep track of the maximum length of the segment seen so far and return the left and right indices of this segment.
}

@ -0,0 +1,8 @@
[package]
name = "check-if-all-the-integers-in-a-range-are-covered"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,22 @@
fn is_covered(ranges: Vec<Vec<i32>>, left: i32, right: i32) -> bool {
let mut line = vec![0; 52]; //
for r in ranges {
line[r[0] as usize] += 1;
line[r[1] as usize + 1] -= 1;
}
let mut overlaps = 0;
for i in 1..=right {
overlaps += line[i as usize];
if i >= left && overlaps == 0 {
return false;
}
}
true
}
fn main() {
let ranges = vec![vec![1, 2], vec![3, 4], vec![5, 6]];
let left = 2;
let right = 7;
println!("{}", is_covered(ranges, left, right));
}

@ -0,0 +1,45 @@
// https://codeforces.com/contest/616/problem/D?locale=en
use std::collections::HashMap;
use std::io::{self, Read};
fn main() {
let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer).unwrap();
let mut lines = buffer.lines();
let _ = lines.next();
let a: Vec<i32> = lines
.next()
.unwrap()
.split_whitespace()
.map(|x| x.parse().unwrap())
.collect();
let k: i32 = lines.next().unwrap().parse().unwrap();
let mut map = HashMap::new();
let mut max = 0;
let mut max_l = 0;
let mut max_r = 0;
let mut l = 0;
let mut r = 0;
while r < a.len() {
let count = map.entry(a[r]).or_insert(0);
*count += 1;
while map.len() > k as usize {
let count = map.get_mut(&a[l]).unwrap();
*count -= 1;
if *count == 0 {
map.remove(&a[l]);
}
l += 1;
}
if r - l + 1 > max {
max = r - l + 1;
max_l = l;
max_r = r;
}
r += 1;
}
println!("{} {}", max_l + 1, max_r + 1);
}
Loading…
Cancel
Save