結果

問題 No.929 よくあるボールを移動するやつ
ユーザー phsplsphspls
提出日時 2020-01-26 18:59:05
言語 Rust
(1.77.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,525 bytes
コンパイル時間 1,491 ms
コンパイル使用メモリ 147,408 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-12 12:22:25
合計ジャッジ時間 2,404 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 4 ms
4,352 KB
testcase_09 AC 4 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 4 ms
4,352 KB
testcase_12 AC 4 ms
4,352 KB
testcase_13 AC 4 ms
4,352 KB
testcase_14 AC 4 ms
4,352 KB
testcase_15 AC 4 ms
4,372 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `n`
 --> 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

warning: 1 warning emitted

ソースコード

diff #

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);
}
0