Add solutions for round 171

This commit is contained in:
Ivan R. 2024-10-28 22:37:34 +05:00
parent 63447f8563
commit 2493382785
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
8 changed files with 266 additions and 0 deletions

View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "problem_a"
version = "0.1.0"

View file

@ -0,0 +1,6 @@
[package]
name = "problem_a"
version = "0.1.0"
edition = "2021"
[dependencies]

View file

@ -0,0 +1,60 @@
# A. Perpendicular Segments
[Codeforces](https://codeforces.com/contest/2026/problem/A)
You are given a coordinate plane and three integers `X`, `Y`, and `K`. Find two line segments `AB` and `CD` such that
1. the coordinates of points `A`, `B`, `C`, and `D` are integers;
2. 0 ≤ `Ax`, `Bx`, `Cx`, `Dx``X` and 0 ≤ `Ay`, `By`, `Cy`, `Dy``Y`;
3. the length of segment `AB` is at least `K`;
4. the length of segment `CD` is at least `K`;
5. segments `AB` and `CD` are perpendicular: if you draw lines that contain `AB` and `CD`, they will cross at a right angle.
Note that it's not necessary for segments to intersect.
Segments are perpendicular as long as the lines they induce are perpendicular.
## Input
The first line contains a single integer `t` (1 ≤ `t` ≤ 5000) — the number of test cases. Next, `t` cases follow.
The first and only line of each test case contains three integers `X`, `Y`, and `K`
(1 ≤ `X`, `Y` ≤ 1000; 1 ≤ `K`≤ 1414).
Additional constraint on the input: the values of `X`, `Y`, and `K`
are chosen in such a way that the answer exists.
## Output
For each test case, print two lines. The first line should contain 4
integers `Ax`, `Ay`, `Bx`, and `By` — the coordinates of the first segment.
The second line should also contain 4
integers `Cx`, `Cy`, `Dx`, and `Dy` — the coordinates of the second segment.
If there are multiple answers, print any of them.
## Example
Input
```
4
1 1 1
3 4 1
4 3 3
3 4 4
```
Output
```
0 0 1 0
0 0 0 1
2 4 2 2
0 1 1 1
0 0 1 3
1 2 4 1
0 1 3 4
0 3 3 0
```

View file

@ -0,0 +1,41 @@
use std::cmp;
use std::io;
struct Segment {
start: Point,
end: Point,
}
struct Point {
x: u64,
y: u64,
}
fn gen_segments(x: u64, y: u64, _k: u64) -> Vec<Segment> {
let min_coord = cmp::min(x, y);
vec!(
Segment{start: Point{x: 0, y: 0}, end: Point{x: min_coord, y: min_coord}},
Segment{start: Point{x: 0, y: min_coord}, end: Point{x: min_coord, y: 0}},
)
}
fn main() {
let stdin = io::stdin();
let mut buffer = String::new();
stdin.read_line(&mut buffer).unwrap();
let n: u64 = buffer.trim().parse().unwrap();
for _i in 0..n {
buffer = String::from("");
stdin.read_line(&mut buffer).unwrap();
let nums = buffer
.trim()
.split_whitespace()
.map(|s| s.parse::<u64>().unwrap())
.collect::<Vec<u64>>();
for segment in gen_segments(nums[0], nums[1], nums[2]) {
println!("{} {} {} {}", segment.start.x, segment.start.y, segment.end.x, segment.end.y);
}
}
}

View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "problem_b"
version = "0.1.0"

View file

@ -0,0 +1,6 @@
[package]
name = "problem_b"
version = "0.1.0"
edition = "2021"
[dependencies]

View file

@ -0,0 +1,65 @@
# B. Black Cells
[Codeforces](https://codeforces.com/contest/2026/problem/B)
You are given a strip divided into cells, numbered from left to right from 0 to 1018. Initially, all cells are white.
You can perform the following operation: choose two white cells `i` and `j`,
such that `i``j` and |`i` `j`| ≤ `k`, and paint them black.
A list `a` is given. All cells from this list must be painted black.
Additionally, at most one cell that is not in this list can also be painted black.
Your task is to determine the minimum value of `k` for which this is possible.
## Input
The first line contains a single integer `t` (1 ≤ `t` ≤ 500) — the number of test cases.
The first line of each test case contains a single integer `n` (1 ≤ `n` ≤ 2000).
The second line contains `n` integers `a1`, `a2`, ... , `an` (0 < `ai`< 1018; `ai` < `ai` + 1).
Additional constraint on the input: the sum of `n` across all test cases does not exceed 2000.
## Output
For each test case, print a single integer — the minimum value of `k`
for which it is possible to paint all the given cells black.
## Example
Input
```
4
2
1 2
1
7
3
2 4 9
5
1 5 8 10 13
```
Output
```
1
1
2
3
```
## Note
In the first example, with `k` = 1 , it is possible to paint the cells (1, 2).
In the second example, with `k` = 1, it is possible to paint the cells (7, 8).
In the third example, with `k` = 2, it is possible to paint the cells (2, 4) and (8, 9).
In the fourth example, with `k` = 3, it is possible to paint the cells (0, 1), (5, 8) and (10, 13).

View file

@ -0,0 +1,74 @@
use std::io;
fn get_max_distance(nums: Vec<u64>) -> u64 {
let mut max_distance = 0;
let mut i = 0;
while i + 1 < nums.len() {
let dist = nums[i + 1] - nums[i];
if dist > max_distance {
max_distance = dist;
}
i += 2;
}
max_distance
}
fn get_min_k(nums: Vec<u64>) -> u64 {
if nums.len() <= 1 {
return 1;
}
if nums.len() % 2 == 0 {
return get_max_distance(nums);
}
let mut min_k = nums.last().unwrap() - nums[0];
for skip_idx in 0..nums.len() {
let mut subset = nums.clone();
subset.remove(skip_idx);
let k = get_max_distance(subset);
if k < min_k {
min_k = k;
}
}
min_k
}
fn main() {
let stdin = io::stdin();
let mut buffer = String::new();
let _ = stdin.read_line(&mut buffer);
let t: u64 = buffer.trim().parse().unwrap();
for _i in 0..t {
let _ = stdin.read_line(&mut buffer);
buffer = String::from("");
let _ = stdin.read_line(&mut buffer);
let nums = buffer
.trim()
.split_whitespace()
.map(|s| s.parse::<u64>().unwrap())
.collect::<Vec<u64>>();
println!("{}", get_min_k(nums));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_samples() {
assert_eq!(get_min_k(vec!(1, 2)), 1);
assert_eq!(get_min_k(vec!(7)), 1);
assert_eq!(get_min_k(vec!(2, 4, 9)), 2);
assert_eq!(get_min_k(vec!(1, 5, 8, 10, 13)), 3);
assert_eq!(get_min_k(vec!(1, 5, 8, 40, 43)), 3);
assert_eq!(get_min_k(vec!(5, 8, 20, 40, 43)), 3);
assert_eq!(get_min_k(vec!(1, 3)), 2);
}
}