結果
| 問題 |
No.929 よくあるボールを移動するやつ
|
| ユーザー |
|
| 提出日時 | 2020-01-26 18:59:05 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 2,000 ms |
| コード長 | 1,525 bytes |
| コンパイル時間 | 15,595 ms |
| コンパイル使用メモリ | 378,352 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-14 11:17:07 |
| 合計ジャッジ時間 | 14,442 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 12 |
コンパイルメッセージ
warning: unused variable: `n` --> src/main.rs:8:9 | 8 | let n: usize = all_data.iter().next().unwrap().parse::<usize>().unwrap(); | ^ help: if this is intentional, prefix it with an underscore: `_n` | = note: `#[warn(unused_variables)]` on by default
ソースコード
use std::io::Read;
use std::collections::VecDeque;
fn main() {
let mut all_data = String::new();
std::io::stdin().read_to_string(&mut all_data).ok();
let all_data: Vec<&str> = all_data.trim().split('\n').map(|s| s.trim()).collect();
let n: usize = all_data.iter().next().unwrap().parse::<usize>().unwrap();
let b: Vec<usize> = all_data.iter().skip(1).next().unwrap().split_whitespace().map(|s| s.parse::<usize>().unwrap()).collect();
let mut empty_indices = VecDeque::new();
let mut overed_indices = VecDeque::new();
let mut cost: usize = 0;
b.iter().enumerate().for_each(|pair| {
let index = pair.0;
let count = pair.1;
if count == &0 {
if overed_indices.is_empty() {
empty_indices.push_back(index);
} else {
cost += index - overed_indices.pop_back().unwrap();
}
} else if count > &1 {
if empty_indices.is_empty() {
(0..(count - 1)).for_each(|_| {
overed_indices.push_back(index);
});
} else {
let mut iter_count = count - 1;
while !empty_indices.is_empty() && iter_count > 0 {
cost += index - empty_indices.pop_front().unwrap();
iter_count -= 1;
}
for _ in 0..iter_count {
overed_indices.push_back(index);
}
}
}
});
println!("{}", cost);
}