結果

問題 No.1053 ゲーミング棒
ユーザー tentententen
提出日時 2021-01-19 14:53:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 769 ms / 2,000 ms
コード長 919 bytes
コンパイル時間 4,546 ms
コンパイル使用メモリ 76,616 KB
実行使用メモリ 82,624 KB
最終ジャッジ日時 2023-08-22 12:26:55
合計ジャッジ時間 15,152 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
55,792 KB
testcase_01 AC 126 ms
55,736 KB
testcase_02 AC 128 ms
55,756 KB
testcase_03 AC 129 ms
55,680 KB
testcase_04 AC 134 ms
55,736 KB
testcase_05 AC 126 ms
55,744 KB
testcase_06 AC 128 ms
55,664 KB
testcase_07 AC 131 ms
55,664 KB
testcase_08 AC 124 ms
54,180 KB
testcase_09 AC 124 ms
55,720 KB
testcase_10 AC 124 ms
55,708 KB
testcase_11 AC 125 ms
55,716 KB
testcase_12 AC 125 ms
55,832 KB
testcase_13 AC 128 ms
55,824 KB
testcase_14 AC 125 ms
55,888 KB
testcase_15 AC 126 ms
55,728 KB
testcase_16 AC 124 ms
55,732 KB
testcase_17 AC 122 ms
55,728 KB
testcase_18 AC 127 ms
55,428 KB
testcase_19 AC 125 ms
55,716 KB
testcase_20 AC 127 ms
55,716 KB
testcase_21 AC 124 ms
55,660 KB
testcase_22 AC 128 ms
55,704 KB
testcase_23 AC 689 ms
82,440 KB
testcase_24 AC 769 ms
81,332 KB
testcase_25 AC 753 ms
81,572 KB
testcase_26 AC 763 ms
82,624 KB
testcase_27 AC 129 ms
55,684 KB
testcase_28 AC 125 ms
55,732 KB
testcase_29 AC 129 ms
55,648 KB
testcase_30 AC 458 ms
60,072 KB
testcase_31 AC 480 ms
60,464 KB
testcase_32 AC 453 ms
60,732 KB
testcase_33 AC 489 ms
60,804 KB
testcase_34 AC 463 ms
60,396 KB
testcase_35 AC 505 ms
60,664 KB
testcase_36 AC 494 ms
60,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayDeque<Integer> deq = new ArrayDeque<>();
		HashMap<Integer, ArrayList<Integer>> positions = new HashMap<>();
		int idx = 0;
		for (int i = 0; i < n; i++) {
		    int x = sc.nextInt();
		    if (deq.size() == 0 || deq.peekLast() != x) {
		        if (!positions.containsKey(x)) {
		            positions.put(x, new ArrayList<>());
		        }
		        positions.get(x).add(idx);
		        deq.add(x);
		        idx++;
		    }
		}
		int count = 0;
		for (ArrayList<Integer> list : positions.values()) {
		    if (list.size() <= 1) {
		        continue;
		    }
		    if (list.size() == 2 && list.get(0) == 0 && list.get(1) == idx - 1) {
		        count++;
		        continue;
		    }
		    System.out.println(-1);
		    return;
		}
		System.out.println(count);
	}
}
0