complete 2083.rs 2530.rs 2721.rs 4128.rs 5584.rs 7512.rs 21519.rs 21964.rs 22862.rs 22973.rs 24123.rs 26993.rs 27514.rs 33849.rs

This commit is contained in:
2026-03-08 23:29:01 +09:00
parent 12445715dd
commit 94591886d6
14 changed files with 899 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
use std::io::stdin;
pub struct Person {
name: String,
age: u32,
weight: u32,
}
impl Person {
fn is_adult(&self) -> bool {
self.age >= 18 || self.weight >= 80
}
fn is_null(&self) -> bool {
self.age == 0 && self.weight == 0
}
}
fn main() {
let mut line = String::new();
loop {
line.clear();
stdin().read_line(&mut line).unwrap();
let mut iter = line.trim_ascii_end().split(' ');
let p = Person {
name: iter.next().unwrap().to_string(),
age: iter.next().unwrap().parse().unwrap(),
weight: iter.next().unwrap().parse().unwrap(),
};
if p.is_null() {
break;
} else if p.is_adult() {
println!("{} Senior", p.name);
} else {
println!("{} Junior", p.name);
}
}
}

View File

@@ -0,0 +1,46 @@
use std::io::{read_to_string, stdin};
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<i64>().unwrap());
let n = iter.next().unwrap() as usize;
let movies = (0..n)
.map(|i| {
(
i,
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
)
})
.collect::<Vec<_>>();
let mut res: i64 = movies.iter().map(|x| x.1).sum();
let mut secondary_movies = movies.iter().map(|x| (x.0, x.2 - x.1)).collect::<Vec<_>>();
secondary_movies.sort_by_key(|x| x.1);
let mut third_movies = movies.iter().map(|x| (x.0, x.3 - x.1)).collect::<Vec<_>>();
third_movies.sort_by_key(|x| x.1);
let s1 = secondary_movies[secondary_movies.len() - 1];
let s2 = secondary_movies[secondary_movies.len() - 2];
let t1 = third_movies[third_movies.len() - 1];
let t2 = third_movies[third_movies.len() - 2];
let cand = if s1.0 == t1.0 {
if s1.1 + t2.1 > s2.1 + t1.1 {
res += s1.1 + t2.1;
(s1.0, t2.0)
} else {
res += s2.1 + t1.1;
(s2.0, t1.0)
}
} else {
res += s1.1 + t1.1;
(s1.0, t1.0)
};
println!("{}", res);
println!("{} {}", cand.0 + 1, cand.1 + 1);
}

View File

@@ -0,0 +1 @@
fn main(){let t=std::io::read_to_string(std::io::stdin()).unwrap();let s=t.split_whitespace().last().unwrap();print!("{}", &s[s.len()-5..])}

View File

@@ -0,0 +1,50 @@
use std::{io::read_to_string, io::stdin};
// 1 2 3 4 5
// 0 1 3
//
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<usize>().unwrap());
let (n, k) = (iter.next().unwrap(), iter.next().unwrap());
let arr = {
let mut arr = vec![];
let org = (0..n).map(|_| iter.next().unwrap() % 2).collect::<Vec<_>>();
let mut cnt = 0;
for ele in org {
if ele == 0 {
arr.push(cnt);
cnt = 0;
} else {
cnt += ele;
}
}
arr
};
let cum_arr = {
let mut carr = vec![0];
for ele in arr.iter() {
carr.push(carr.last().unwrap() + ele);
}
carr
};
let mut lo = 1;
let mut hi = 1;
let mut res = 0;
while hi < cum_arr.len() {
let key = cum_arr[hi] - cum_arr[lo];
let len = hi - lo + 1;
if key > k {
lo += 1;
} else {
res = res.max(len);
hi += 1;
}
}
println!("{}", res);
}

View File

@@ -0,0 +1,26 @@
use std::io::{read_to_string, stdin};
fn main() {
let k = read_to_string(stdin())
.unwrap()
.trim()
.parse::<i64>()
.unwrap();
let k = k.abs() as u64;
if k == 0 {
println!("{}", 0);
}
else if k % 2 == 0 {
println!("{}", -1);
} else {
let mut max_i = 48;
for i in 0..=48 {
if k >> i == 0 {
max_i = i;
break;
}
}
println!("{}", max_i);
}
}

View File

@@ -0,0 +1,71 @@
use std::{
cmp::max,
io::{read_to_string, stdin},
};
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp.split_ascii_whitespace();
let (n, m, t, x, y) = (
iter.next().unwrap().parse::<usize>().unwrap(),
iter.next().unwrap().parse::<usize>().unwrap(),
iter.next().unwrap().parse::<i64>().unwrap(),
iter.next().unwrap().parse::<i64>().unwrap(),
iter.next().unwrap().parse::<i64>().unwrap(),
);
let points = (0..m)
.map(|_| iter.next().unwrap().parse::<i64>().unwrap())
.collect::<Vec<_>>();
let mut logs = (0..y)
.map(|_| {
(
iter.next().unwrap().parse::<i64>().unwrap(), // time
iter.next().unwrap().parse::<usize>().unwrap() - 1, // participant
iter.next().unwrap().parse::<usize>().unwrap() - 1, // problem num
match iter.next().unwrap().chars().next().unwrap() {
'o' => 0, // open
'c' => 1, // corr
'i' => 2, // incr
_ => 0,
},
)
})
.collect::<Vec<_>>();
logs.sort_by_key(|x| x.0); // sort by time
let mut open_times = vec![vec![0; m]; n];
let mut corr_times = vec![vec![-1; m]; n];
let mut wrong_trials = vec![vec![0; m]; n];
for log in logs {
let (time, part, prob, delta) = log;
match delta {
0 => {
open_times[part][prob] = time;
}
1 => {
corr_times[part][prob] = time;
}
2 => {
wrong_trials[part][prob] += 1;
}
_ => {}
}
}
for part in 0..n {
let mut s = 0;
for prob in 0..m {
if corr_times[part][prob] == -1 {
continue; // 0
}
s += max(
points[prob]
- (corr_times[part][prob] - open_times[part][prob])
- 120 * wrong_trials[part][prob],
x,
);
}
println!("{}", s);
}
}

View File

@@ -0,0 +1,32 @@
use std::io::{read_to_string, stdin};
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|s| s.parse::<usize>().unwrap());
let (mut h, mut m, mut s) = (
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
);
let cooking_time = iter.next().unwrap();
for _ in 0..cooking_time {
s += 1;
if s == 60 {
s = 0;
m += 1;
}
if m == 60 {
m = 0;
h += 1;
}
if h == 24 {
h = 0;
}
}
println!("{} {} {}", h, m, s);
}

View File

@@ -0,0 +1,121 @@
use std::{
collections::VecDeque,
io::{read_to_string, stdin},
};
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<usize>().unwrap());
let (w, h) = (iter.next().unwrap(), iter.next().unwrap());
let mut start = (0, 0);
let mut dest = (0, 0);
let field = (0..h)
.map(|i| {
(0..w)
.map(|j| {
let t = iter.next().unwrap();
if t == 2 {
start = (i, j);
} else if t == 3 {
dest = (i, j);
}
t
})
.collect()
})
.collect::<Vec<Vec<_>>>();
let deltas = [(-1, 0), (1, 0), (0, -1), (0, 1)];
let mut weight_map = vec![vec![0; w]; h];
{
let mut vis = vec![vec![false; w]; h];
let mut deq = VecDeque::new();
deq.push_back((start.clone(), 0));
while !deq.is_empty() {
let (curr, curr_weight) = deq.pop_front().unwrap();
if vis[curr.0][curr.1] {
continue;
} else {
vis[curr.0][curr.1] = true;
}
let curr_state = field[curr.0][curr.1];
if curr_state == 4 {
weight_map[curr.0][curr.1] += curr_weight;
}
for (dx, dy) in deltas {
let nxt = (curr.0 as isize + dx, curr.1 as isize + dy);
if nxt.0 < 0 || nxt.0 >= h as isize || nxt.1 < 0 || nxt.1 >= w as isize {
continue;
}
let nxt = (nxt.0 as usize, nxt.1 as usize);
if field[nxt.0][nxt.1] == 1 {
continue;
}
deq.push_back((nxt, curr_weight + 1));
}
}
}
{
let mut vis = vec![vec![false; w]; h];
let mut deq = VecDeque::new();
deq.push_back((dest.clone(), 0));
while !deq.is_empty() {
let (curr, curr_weight) = deq.pop_front().unwrap();
if vis[curr.0][curr.1] {
continue;
} else {
vis[curr.0][curr.1] = true;
}
let curr_state = field[curr.0][curr.1];
if curr_state == 4 {
weight_map[curr.0][curr.1] += curr_weight;
}
for (dx, dy) in deltas {
let nxt = (curr.0 as isize + dx, curr.1 as isize + dy);
if nxt.0 < 0 || nxt.0 >= h as isize || nxt.1 < 0 || nxt.1 >= w as isize {
continue;
}
let nxt = (nxt.0 as usize, nxt.1 as usize);
if field[nxt.0][nxt.1] == 1 {
continue;
}
deq.push_back((nxt, curr_weight + 1));
}
}
}
let mut res = usize::MAX;
for i in 0..h {
for j in 0..w {
let t = weight_map[i][j];
if t == 0 {
continue;
}
res = res.min(t);
}
}
println!("{}", res);
}

View File

@@ -0,0 +1,24 @@
use std::io::{read_to_string, stdin};
fn W(n: usize) -> usize {
// W(1) = 1 * T(2) = 3
// W(2) = 1 * T(2) + 2 * T(3) = 12 + 3 = 15
// W(n + 1) - W(n) = n * T(n + 1) = n * (n + 1) * (n + 2) / 2
if n == 1 {
3
} else {
n * (n + 1) * (n + 2) / 2 + W(n - 1)
}
}
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<usize>().unwrap());
(0..iter.next().unwrap()).for_each(|_| {
let n = iter.next().unwrap();
println!("{}", W(n));
});
}

View File

@@ -0,0 +1,32 @@
use std::{
cmp::Reverse,
collections::BinaryHeap,
io::{read_to_string, stdin},
};
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<usize>().unwrap());
let n = iter.next().unwrap();
let arr = (0..n)
.map(|_| Reverse(iter.next().unwrap()))
.filter(|x| x.0 != 0)
.collect::<Vec<_>>();
let mut heap = BinaryHeap::from(arr);
while (heap.len() >= 2) {
let a = heap.pop().unwrap().0;
let b = heap.pop().unwrap().0;
if a != b {
heap.push(Reverse(a.max(b)));
} else {
heap.push(Reverse(a + b));
}
}
let res = heap.iter().max().unwrap().0;
println!("{}", res);
}

View File

@@ -0,0 +1,92 @@
use std::{
cmp::Ordering,
io::{read_to_string, stdin},
};
fn gcd(a: u64, b: u64) -> u64 {
let mut r = 0;
let mut a = a;
let mut b = b;
while b > 0 {
r = a % b;
a = b;
b = r;
}
a
}
#[derive(Debug)]
pub struct Rational {
p: u64,
q: u64,
}
impl Rational {
fn new(p: u64, q: u64) -> Self {
Rational {
p: p / gcd(p, q),
q: q / gcd(p, q),
}
}
}
impl PartialEq for Rational {
fn eq(&self, other: &Self) -> bool {
self.p == other.p && self.q == other.q
}
fn ne(&self, other: &Self) -> bool {
self.p != other.p && self.q != other.q
}
}
impl PartialOrd for Rational {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some((self.p * other.q).cmp(&(other.p * self.q)))
}
}
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<u64>().unwrap());
let n = iter.next().unwrap() as usize;
let xs = (0..n).map(|_| iter.next().unwrap()).collect::<Vec<_>>();
let ys = (0..n).map(|_| iter.next().unwrap()).collect::<Vec<_>>();
let rs = xs
.iter()
.zip(ys.iter())
.map(|(&x, &y)| Rational::new(y, x))
.collect::<Vec<_>>();
let mut max_value = Rational { p: 0, q: 1 };
let mut max_cons = 0;
let mut curr_cons = 0;
for i in rs {
if i > max_value {
curr_cons = 1;
max_cons = 0;
max_value = i;
} else if i == max_value {
if curr_cons > 0 {
curr_cons += 1;
} else {
curr_cons += 1;
}
} else {
max_cons = max_cons.max(curr_cons);
curr_cons = 0;
}
}
if curr_cons > 0 {
max_cons = max_cons.max(curr_cons);
}
println!("{} {}", max_value.p, max_value.q);
println!("{}", max_cons);
}

View File

@@ -0,0 +1,170 @@
use std::{
cmp::min,
collections::HashMap,
io::{read_to_string, stdin},
};
fn remap(x: (i64, i64, i64, i64)) -> (i64, i64, i64, i64) {
let (x1, y1, x2, y2) = x;
let x = if x1 < x2 { (x1, x2) } else { (x2, x1) };
let y = if y1 < y2 { (y1, y2) } else { (y2, y1) };
(x.0, y.0, x.1, y.1)
}
fn is_bound_out(xf0: i64, xf1: i64, x: i64) -> bool {
x < xf0 || x > xf1
}
fn compensate_bounded_out(xf0: i64, xf1: i64, x: i64) -> i64 {
if x < xf0 {
xf0
} else if x > xf1 {
xf1
} else {
x
}
}
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<i64>().unwrap());
let plate = remap((
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
));
let n = iter.next().unwrap() as usize;
let chips = (0..n)
.map(|_| {
(
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
)
})
.map(remap)
.collect::<Vec<_>>();
let (h_map, width) = {
let mut h_map: HashMap<i64, usize> = HashMap::new();
let h_base = vec![plate.0, plate.2].into_iter();
let h1 = chips
.iter()
.map(|&(x, _, _, _)| x)
.filter(|x| !is_bound_out(plate.0, plate.2, *x));
let h2 = chips
.iter()
.map(|&(_, _, x, _)| x)
.filter(|x| !is_bound_out(plate.0, plate.2, *x));
let mut h = h_base.chain(h1).chain(h2).collect::<Vec<_>>();
h.sort();
let mut cnt = 0;
for &v in h.iter() {
if !h_map.contains_key(&v) {
h_map.insert(v, cnt);
cnt += 1;
}
}
(h_map, cnt)
};
let (v_map, height) = {
let mut v_map: HashMap<i64, usize> = HashMap::new();
let v_base = vec![plate.1, plate.3].into_iter();
let v1 = chips
.iter()
.map(|&(_, x, _, _)| x)
.filter(|x| !is_bound_out(plate.1, plate.3, *x));
let v2 = chips
.iter()
.map(|&(_, _, _, x)| x)
.filter(|x| !is_bound_out(plate.1, plate.3, *x));
let mut v = v_base.chain(v1).chain(v2).collect::<Vec<_>>();
v.sort();
let mut cnt = 0;
for &val in v.iter() {
if !v_map.contains_key(&val) {
v_map.insert(val, cnt);
cnt += 1;
}
}
(v_map, cnt)
};
let chips = chips
.into_iter()
.map(|(x1, y1, x2, y2)| {
(
h_map[&compensate_bounded_out(plate.0, plate.2, x1)],
v_map[&compensate_bounded_out(plate.1, plate.3, y1)],
h_map[&compensate_bounded_out(plate.0, plate.2, x2)],
v_map[&compensate_bounded_out(plate.1, plate.3, y2)],
)
})
.collect::<Vec<_>>();
let mut field = vec![vec![false; height - 1]; width - 1];
for chip in chips {
let (x1, y1, x2, y2) = chip;
for i in x1..x2 {
for j in y1..y2 {
field[i][j] = true;
}
}
}
let mut vis = field.clone();
let delta = [(0, -1), (0, 1), (-1, 0), (1, 0)];
let mut res = 0;
for i in 0..width - 1 {
for j in 0..height - 1 {
if vis[i][j] {
continue;
}
res += 1;
let mut deq = vec![(i, j)];
while !deq.is_empty() {
let curr = deq.pop().unwrap();
if vis[curr.0][curr.1] {
continue;
} else {
vis[curr.0][curr.1] = true;
}
for dx in delta {
let nxt = (curr.0 as isize + dx.0, curr.1 as isize + dx.1);
if !(nxt.0 < 0
|| nxt.0 >= width as isize - 1
|| nxt.1 < 0
|| nxt.1 >= height as isize - 1)
{
let nxt = (nxt.0 as usize, nxt.1 as usize);
if !field[nxt.0][nxt.1] {
deq.push((nxt.0 as usize, nxt.1 as usize));
}
}
}
}
}
}
println!("{}", res);
}

View File

@@ -0,0 +1,117 @@
use std::{
collections::HashMap,
io::{read_to_string, stdin},
};
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<usize>().unwrap());
let (w, h) = (iter.next().unwrap(), iter.next().unwrap());
let n = iter.next().unwrap();
let tapes = (0..n)
.map(|_| {
(
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
iter.next().unwrap(),
)
})
.collect::<Vec<_>>();
let mut horizontal_map = HashMap::new();
let mut vertical_map = HashMap::new();
let mut horionztal_pivots = vec![];
let mut vertical_pivots = vec![];
horionztal_pivots.push(0);
horionztal_pivots.push(w);
vertical_pivots.push(0);
vertical_pivots.push(h);
for &(x1, y1, x2, y2) in tapes.iter() {
horionztal_pivots.push(x1);
horionztal_pivots.push(x2);
vertical_pivots.push(y1);
vertical_pivots.push(y2);
}
horionztal_pivots.sort();
vertical_pivots.sort();
let mut cnt = 0;
for pivot in horionztal_pivots.iter() {
if !horizontal_map.contains_key(pivot) {
horizontal_map.insert(*pivot, cnt);
cnt += 1;
}
}
let width = cnt - 1;
let mut cnt = 0;
for pivot in vertical_pivots.iter() {
if !vertical_map.contains_key(pivot) {
vertical_map.insert(*pivot, cnt);
cnt += 1;
}
}
let height = cnt - 1;
let mut field = vec![vec![false; height]; width];
for (x1, y1, x2, y2) in tapes.iter() {
let (x1, y1, x2, y2) = (
horizontal_map[x1],
vertical_map[y1],
horizontal_map[x2],
vertical_map[y2],
);
for i in x1..x2 {
for j in y1..y2 {
field[i][j] = true;
}
}
}
let mut res = 0;
let mut vis = field.clone();
let delta = vec![(0, -1), (0, 1), (-1, 0), (1, 0)];
for i in 0..width {
for j in 0..height {
if vis[i][j] {
continue;
}
let mut deq = vec![(i, j)];
while !deq.is_empty() {
let curr = deq.pop().unwrap();
if vis[curr.0][curr.1] {
continue;
} else {
vis[curr.0][curr.1] = true;
}
for (dx, dy) in delta.iter() {
let nxt_x = curr.0 as isize + dx;
if nxt_x < 0 || nxt_x >= width as isize {
continue;
}
let nxt_y = curr.1 as isize + dy;
if nxt_y < 0 || nxt_y >= height as isize {
continue;
}
if vis[nxt_x as usize][nxt_y as usize] {
continue;
}
deq.push((nxt_x as usize, nxt_y as usize));
}
}
res += 1;
}
}
println!("{}", res);
}

View File

@@ -0,0 +1,78 @@
use std::io::{read_to_string, stdin};
const P_MAX: usize = 10_000_000;
fn get_primes(n: usize) -> Vec<usize> {
let mut primes = vec![2];
for i in (3..=n).step_by(2) {
for &p in primes.iter() {
if i % p == 0 {
break;
}
if p * p >= i {
primes.push(i);
break;
}
}
}
primes
}
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<usize>().unwrap());
let tc = iter.next().unwrap();
let primes = get_primes(P_MAX);
let cum_primes = {
let mut cp = vec![0];
for p in primes.iter() {
cp.push(cp.last().unwrap() + p);
}
cp
};
(1..=tc).for_each(|case| {
let m = iter.next().unwrap();
let ns = (0..m).map(|_| iter.next().unwrap()).collect::<Vec<_>>();
let mut idxs = vec![0; m];
let mut values = vec![0; m];
let mut _cnt = 0;
loop {
let finish_flag = values[0] != 0 && values.iter().all(|&x| x == values[0]);
if finish_flag {
break;
}
let i = values
.iter()
.enumerate()
.min_by_key(|&(_, &v)| v)
.unwrap()
.0;
let n = ns[i];
let mut idx = idxs[i];
loop {
let s = cum_primes[idx + n] - cum_primes[idx];
match primes.binary_search(&s) {
Ok(x) => {
values[i] = primes[x];
idxs[i] = idx + 1;
break;
}
Err(_) => {}
}
idx += 1;
}
}
println!("Scenario {}:\n{}\n", case, values[0]);
})
}