結果

問題 No.1765 While Shining
ユーザー ks2mks2m
提出日時 2021-11-26 22:36:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 610 ms / 2,000 ms
コード長 798 bytes
コンパイル時間 2,054 ms
コンパイル使用メモリ 73,884 KB
実行使用メモリ 67,444 KB
最終ジャッジ日時 2023-09-12 05:15:16
合計ジャッジ時間 14,131 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,384 KB
testcase_01 AC 120 ms
56,092 KB
testcase_02 AC 121 ms
55,636 KB
testcase_03 AC 120 ms
55,796 KB
testcase_04 AC 141 ms
55,512 KB
testcase_05 AC 172 ms
56,080 KB
testcase_06 AC 150 ms
56,240 KB
testcase_07 AC 148 ms
55,712 KB
testcase_08 AC 176 ms
56,240 KB
testcase_09 AC 504 ms
60,760 KB
testcase_10 AC 597 ms
64,824 KB
testcase_11 AC 530 ms
62,752 KB
testcase_12 AC 575 ms
66,092 KB
testcase_13 AC 539 ms
62,588 KB
testcase_14 AC 575 ms
67,444 KB
testcase_15 AC 605 ms
64,888 KB
testcase_16 AC 597 ms
65,044 KB
testcase_17 AC 594 ms
64,820 KB
testcase_18 AC 589 ms
64,716 KB
testcase_19 AC 584 ms
65,636 KB
testcase_20 AC 606 ms
65,092 KB
testcase_21 AC 610 ms
62,172 KB
testcase_22 AC 593 ms
66,128 KB
testcase_23 AC 602 ms
65,420 KB
testcase_24 AC 609 ms
64,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		sc.close();

		if (n == 1) {
			System.out.println(0);
			return;
		}

		long[] num = new long[n + 1];
		num[1] = 1;
		num[2] = 2;
		for (int i = 3; i < num.length; i++) {
			num[i] = num[i - 2] + i;
		}

		long ans = 0;
		int cnt = 0;
		for (int i = 0; i < n - 1; i++) {
			if (cnt % 2 == 0) {
				if (a[i] == 1) {
					cnt++;
				} else {
					ans += num[cnt];
					cnt = 0;
				}
			} else {
				if (a[i] == 1) {
					ans += num[cnt];
					cnt = 1;
				} else {
					cnt++;
				}
			}
		}
		ans += num[cnt];
		System.out.println(ans);
	}
}
0