complete 3192.rs 15340.rs 16434.rs

This commit is contained in:
2026-01-15 17:33:36 -08:00
parent 028357635a
commit 8f4d80c08f
3 changed files with 331 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
use std::io::stdin;
fn min_fee(call: u64, data: u64) -> u64 {
[
call * 30 + data * 40,
call * 35 + data * 30,
call * 40 + data * 20,
]
.into_iter()
.min()
.unwrap()
}
fn main() {
let mut line = String::new();
loop {
line.clear();
stdin().read_line(&mut line).unwrap();
let mut iter = line
.trim_ascii_end()
.split(' ')
.map(|x| x.parse::<u64>().unwrap());
let (call, data) = (iter.next().unwrap(), iter.next().unwrap());
if call == 0 && data == 0 {
break;
} else {
println!("{}", min_fee(call, data));
}
}
}

View File

@@ -0,0 +1,101 @@
use std::cmp::min;
use std::i64;
use std::io::{read_to_string, stdin};
enum Room {
Monster { atk: usize, hp: usize },
Potion { atk: usize, hp: usize },
}
fn battle(atk: usize, mon_atk: usize, mon_hp: usize) -> usize {
let d = mon_hp / atk;
let r = mon_hp % atk;
if d == 0 {
0
} else if r == 0 {
(d - 1) * mon_atk
} else {
d * mon_atk
}
}
fn simulate(hp_max: i64, hp_changes: &Vec<i64>) -> i64 {
let mut hp = hp_max;
for &dhp in hp_changes {
hp += dhp;
if hp <= 0 {
return hp;
}
if hp > hp_max {
hp = hp_max;
}
}
hp
}
fn needed_hp_for_clearing(init_atk: usize, rooms: &Vec<Room>) -> i64 {
let mut atk = init_atk;
let hp_changes = rooms
.iter()
.map(|room| match room {
Room::Monster {
atk: mon_atk,
hp: mon_hp,
} => {
let damage = battle(atk, *mon_atk, *mon_hp) as i64;
-damage as i64
}
Room::Potion { atk: datk, hp: dhp } => {
atk += *datk;
*dhp as i64
}
})
.collect::<Vec<i64>>();
let mut lo = 1;
let mut hi = i64::MAX - 1;
let mut accept_min = hi;
while lo <= hi {
let mid = (lo + hi) / 2;
let final_hp = simulate(mid, &hp_changes);
if final_hp > 0 {
hi = mid - 1;
accept_min = min(accept_min, mid);
} else {
lo = mid + 1;
}
}
accept_min
}
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 init_atk = iter.next().unwrap();
let rooms = (0..n)
.map(|_| {
let t = iter.next().unwrap();
if t == 1 {
Room::Monster {
atk: iter.next().unwrap(),
hp: iter.next().unwrap(),
}
} else {
Room::Potion {
atk: iter.next().unwrap(),
hp: iter.next().unwrap(),
}
}
})
.collect::<Vec<Room>>();
println!("{}", needed_hp_for_clearing(init_atk, &rooms));
}

View File

@@ -0,0 +1,196 @@
use std::io::{read_to_string, stdin};
const SIZE: usize = 3;
fn get_horizontal_with_cnt_zeros(magic_square: &[u64; 9], row: usize) -> (u64, usize) {
let mut sum = 0;
let mut cnt_zeros = 0;
for j in 0..SIZE {
let v = magic_square[row * SIZE + j];
if v == 0 {
cnt_zeros += 1;
}
sum += v;
}
(sum, cnt_zeros)
}
fn get_vertical_with_cnt_zeros(magic_square: &[u64; 9], col: usize) -> (u64, usize) {
let mut sum = 0;
let mut cnt_zeros = 0;
for i in 0..SIZE {
let v = magic_square[i * SIZE + col];
if v == 0 {
cnt_zeros += 1;
}
sum += v;
}
(sum, cnt_zeros)
}
fn get_diagonal1_with_cnt_zeros(magic_square: &[u64; 9]) -> (u64, usize) {
let mut sum = 0;
let mut cnt_zeros = 0;
for i in 0..SIZE {
let v = magic_square[i * SIZE + i];
if v == 0 {
cnt_zeros += 1;
}
sum += v;
}
(sum, cnt_zeros)
}
fn get_diagonal2_with_cnt_zeros(magic_square: &[u64; 9]) -> (u64, usize) {
let mut sum = 0;
let mut cnt_zeros = 0;
for i in 0..SIZE {
let v = magic_square[i * SIZE + (SIZE - 1 - i)];
if v == 0 {
cnt_zeros += 1;
}
sum += v;
}
(sum, cnt_zeros)
}
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<u64>().unwrap());
let mut magic_square = [0; 9];
let mut missings = vec![];
for i in 0..SIZE * SIZE {
let value = iter.next().unwrap();
if value == 0 {
missings.push(i);
}
magic_square[i] = value;
}
let mut res = 0u64;
let mut zero_flag = false;
for i in 0..SIZE {
let mut s1 = 0;
let mut s2 = 0;
let mut z1 = 0;
let mut z2 = 0;
for j in 0..SIZE {
let v1 = magic_square[i * SIZE + j];
if v1 == 0 {
z1 += 1;
}
s1 += v1;
let v2 = magic_square[i + SIZE * j];
if v2 == 0 {
z2 += 1;
}
s2 += v2;
}
if s1 == 0 || s2 == 0 {
zero_flag = true;
break;
}
if z1 == 0 {
res = s1;
break;
} else if z2 == 0 {
res = s2;
break;
}
}
{
let mut s1 = 0;
let mut s2 = 0;
let mut z1 = 0;
let mut z2 = 0;
for i in 0..SIZE {
let v1 = magic_square[i * SIZE + i];
let v2 = magic_square[i * SIZE + (SIZE - 1 - i)];
if v1 == 0 {
z1 += 1;
}
if v2 == 0 {
z2 += 1;
}
s1 += v1;
s2 += v2;
}
if s1 == 0 || s2 == 0 {
zero_flag = true;
}
if z1 == 0 {
res = s1;
} else if z2 == 0 {
res = s2;
}
}
if zero_flag {
res = magic_square.iter().sum::<u64>() / (SIZE as u64 - 1);
}
loop {
let mut cnt_zero = 0;
for i in 0..SIZE * SIZE {
if magic_square[i] == 0 {
cnt_zero += 1;
// fill
let (r, c) = (i / SIZE, i % SIZE);
let (h_sum, h_cnt_zeros) = get_horizontal_with_cnt_zeros(&magic_square, r);
let (v_sum, v_cnt_zeros) = get_vertical_with_cnt_zeros(&magic_square, c);
if h_cnt_zeros == 1 {
magic_square[i] = res - h_sum;
} else if v_cnt_zeros == 1 {
magic_square[i] = res - v_sum;
} else {
if r == c {
let (d1_sum, d1_cnt_zeros) = get_diagonal1_with_cnt_zeros(&magic_square);
if d1_cnt_zeros == 1 {
magic_square[i] = res - d1_sum;
continue;
}
}
if r + c == SIZE - 1 {
let (d2_sum, d2_cnt_zeros) = get_diagonal2_with_cnt_zeros(&magic_square);
if d2_cnt_zeros == 1 {
magic_square[i] = res - d2_sum;
continue;
}
}
}
}
}
if cnt_zero == 0 {
break;
}
}
for i in 0..SIZE {
for j in 0..SIZE {
print!("{} ", magic_square[i * SIZE + j]);
}
println!();
}
}