結果

問題 No.1053 ゲーミング棒
コンテスト
ユーザー phspls
提出日時 2020-07-25 11:04:32
言語 Rust
(1.94.0 + proconio + num + itertools)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
WA  
実行時間 -
コード長 975 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,478 ms
コンパイル使用メモリ 194,932 KB
実行使用メモリ 11,664 KB
最終ジャッジ日時 2026-03-14 00:38:51
合計ジャッジ時間 3,311 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

use std::collections::HashMap;

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut a = String::new();
    std::io::stdin().read_line(&mut a).ok();
    let a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();

    let mut colors: HashMap<usize, Vec<usize>> = HashMap::new();
    let mut prev_color: usize = n+1;
    a.iter().enumerate().for_each(|pair| {
        if prev_color == *pair.1 {
            return;
        }
        if let Some(x) = colors.get_mut(pair.1) {
            x.push(pair.0);
        } else {
            colors.insert(*pair.1, vec![pair.0]);
        }
        prev_color = *pair.1;
    });
    let mut result: usize = 0;
    for v in colors.values() {
        if v.len() > 2 {
            println!("-1");
            return;
        } else if v.len() == 2 {
            result += 1;
        }
    }
    println!("{}", result);
}
0