結果

問題 No.1765 While Shining
ユーザー ryusukeryusuke
提出日時 2022-01-30 17:48:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 1,072 bytes
コンパイル時間 331 ms
コンパイル使用メモリ 87,280 KB
実行使用メモリ 105,272 KB
最終ジャッジ日時 2023-09-02 01:42:19
合計ジャッジ時間 4,815 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,468 KB
testcase_01 AC 71 ms
71,396 KB
testcase_02 AC 71 ms
71,516 KB
testcase_03 AC 72 ms
71,384 KB
testcase_04 AC 70 ms
71,520 KB
testcase_05 AC 72 ms
71,348 KB
testcase_06 AC 70 ms
71,604 KB
testcase_07 AC 71 ms
71,460 KB
testcase_08 AC 73 ms
71,516 KB
testcase_09 AC 104 ms
93,440 KB
testcase_10 AC 119 ms
104,868 KB
testcase_11 AC 109 ms
98,408 KB
testcase_12 AC 115 ms
101,560 KB
testcase_13 AC 108 ms
98,344 KB
testcase_14 AC 118 ms
104,848 KB
testcase_15 AC 120 ms
104,912 KB
testcase_16 AC 120 ms
104,968 KB
testcase_17 AC 117 ms
105,216 KB
testcase_18 AC 120 ms
104,764 KB
testcase_19 AC 118 ms
105,176 KB
testcase_20 AC 119 ms
105,272 KB
testcase_21 AC 119 ms
104,656 KB
testcase_22 AC 118 ms
105,272 KB
testcase_23 AC 116 ms
105,156 KB
testcase_24 AC 106 ms
104,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
a = list(map(int, input().split()))

def calc(n: int, v: int) -> int:
    '溜まった分を計算する'
    if v == 0:
        if n % 2 == 0:
            return n // 2 * (n // 2 + 1)
        elif n % 2 == 1:
            return (n + 1) // 2 * ((n + 1) // 2 + 1) - (n + 1) // 2
    elif v == 1:
        if n % 2 == 0:
            return n // 2 * (n // 2 + 1) - n // 2
        elif n % 2 == 1:
            return (n + 1) // 2 * ((n + 1) // 2 + 1) - (n + 1) // 2 - (n + 1) // 2

ans, cnt, now = 0, 0, -1 # 答え・溜まってる数・今いた場所
for i in range(n):
    if now == -1:
        if a[i] == 1:
            now = 1
            cnt = 1
    else:
        if now == 0 and a[i] == 1:
                now = 1
                cnt += 1
        elif now == 1 and a[i] == 0:
                now = 0
                cnt += 1
        else:
            ans += calc(cnt, 0)
            if a[i] == 0:
                cnt, now = 0, -1
            elif a[i] == 1:
                cnt, now = 1, 1
    #print(i, a[i], cnt,ans)

ans += calc(cnt, 1)
print(ans)
0