-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
32 lines (26 loc) · 877 Bytes
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use std::cmp;
use std::io::{self, BufRead};
fn get_cost(target_position: i32, positions: &Vec<i32>) -> i32 {
let mut cost: i32 = 0;
for position in positions {
let move_num = (target_position - position).abs();
cost += move_num * (move_num + 1) / 2;
}
cost
}
fn main() {
let stdin = io::stdin();
// initial state of fish: [0 timer fish, 1 timer fish, ...]
let positions = String::from_utf8(stdin.lock().fill_buf().unwrap().to_vec())
.unwrap()
.split(",")
.map(|x| x.parse::<i32>().unwrap())
.collect::<Vec<i32>>();
let max_position = positions.iter().max().unwrap();
let mut min_cost = i32::MAX;
for target_position in 0..*max_position {
let cost = get_cost(target_position, &positions);
min_cost = cmp::min(cost, min_cost);
}
println!("ans {}", min_cost);
}