-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1765.py
More file actions
37 lines (34 loc) · 1.01 KB
/
1765.py
File metadata and controls
37 lines (34 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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(ans + calc(cnt, 1))