Skip to content

Commit 41a2505

Browse files
committed
abc351
1 parent 7c41f0b commit 41a2505

File tree

7 files changed

+289
-0
lines changed

7 files changed

+289
-0
lines changed

atcoder/abc/abc301-400/abc351/a.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a = list(map(int, input().split()))
2+
b = list(map(int, input().split()))
3+
4+
print(sum(a) - sum(b) + 1)

atcoder/abc/abc301-400/abc351/b.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
n = int(input())
2+
a = [[i for i in list(str(input()))] for _ in range(n)]
3+
b = [[i for i in list(str(input()))] for _ in range(n)]
4+
5+
ans = [-1, -1]
6+
7+
for i in range(n):
8+
for j in range(n):
9+
if a[i][j] != b[i][j]:
10+
ans = [i + 1, j + 1]
11+
break
12+
if ans != [-1, -1]:
13+
break
14+
print(*ans, sep=" ")

atcoder/abc/abc301-400/abc351/c.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from collections import deque
2+
n = int(input())
3+
a = list(map(int, input().split()))
4+
5+
# 右方向から走査と左方向から走査を一回ずつ
6+
a = deque(a)
7+
tmp_a = deque()
8+
9+
while a:
10+
a_head = a.popleft()
11+
if len(tmp_a) > 0 and tmp_a[-1] == a_head:
12+
a_head += 1
13+
tmp_a.pop()
14+
a.appendleft(a_head)
15+
continue
16+
elif len(a) == 0:
17+
tmp_a.append(a_head)
18+
break
19+
else:
20+
if a[0] == a_head:
21+
a.popleft()
22+
a_head += 1
23+
a.appendleft(a_head)
24+
else:
25+
tmp_a.append(a_head)
26+
27+
a = tmp_a
28+
tmp_a = deque()
29+
30+
while a:
31+
a_tail = a.pop()
32+
if len(tmp_a) > 0 and tmp_a[0] == a_tail:
33+
a_tail += 1
34+
tmp_a.popleft()
35+
a.append(a_tail)
36+
continue
37+
elif len(a) == 0:
38+
tmp_a.appendleft(a_tail)
39+
break
40+
else:
41+
if a[-1] == a_tail:
42+
a.pop()
43+
a_tail += 1
44+
a.append(a_tail)
45+
else:
46+
tmp_a.appendleft(a_tail)
47+
48+
print(len(tmp_a))

atcoder/abc/abc301-400/abc351/d.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import sys
2+
from collections import defaultdict, deque
3+
input = lambda: sys.stdin.readline().rstrip()
4+
5+
h, w = map(int, input().split())
6+
s = [[i for i in list(str(input()))] for _ in range(h)]
7+
8+
class UnionFind():
9+
def __init__(self, n):
10+
self.n = n + 1
11+
self.parents = [-1] * (n + 1)
12+
13+
def find(self, x):
14+
if self.parents[x] < 0:
15+
return x
16+
else:
17+
self.parents[x] = self.find(self.parents[x])
18+
return self.parents[x]
19+
20+
def union(self, x, y):
21+
x = self.find(x)
22+
y = self.find(y)
23+
24+
if x == y:
25+
return
26+
27+
if self.parents[x] > self.parents[y]:
28+
x, y = y, x
29+
30+
self.parents[x] += self.parents[y]
31+
self.parents[y] = x
32+
33+
def size(self, x):
34+
return -self.parents[self.find(x)]
35+
36+
def same(self, x, y):
37+
return self.find(x) == self.find(y)
38+
39+
def members(self, x):
40+
root = self.find(x)
41+
return [i for i in range(self.n) if self.find(i) == root]
42+
43+
def roots(self):
44+
return [i for i, x in enumerate(self.parents) if x < 0]
45+
46+
def group_count(self):
47+
return len(self.roots())
48+
49+
def all_group_members(self):
50+
return {r: self.members(r) for r in self.roots()}
51+
52+
def __str__(self):
53+
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
54+
55+
pos_num = defaultdict(lambda: len(pos_num))
56+
visited = [[False] * w for _ in range(h)]
57+
dxy = [(1, 0), (0, 1), (-1, 0), (0, -1)]
58+
59+
def is_magnet_neighbour(y, x):
60+
for dy, dx in dxy:
61+
ny = y + dy
62+
nx = x + dx
63+
if 0 <= ny < h and 0 <= nx < w and s[ny][nx] == "#":
64+
return True
65+
return False
66+
67+
ans = 0
68+
q = deque()
69+
for i in range(h):
70+
for j in range(w):
71+
if s[i][j] == "#":
72+
visited[i][j] = True
73+
continue
74+
elif visited[i][j]:
75+
continue
76+
else:
77+
q = deque([(i, j)])
78+
visit_path = set([(i, j)])
79+
if not is_magnet_neighbour(i, j):
80+
while q:
81+
y, x = q.popleft()
82+
visited[y][x] = True
83+
for dy, dx in dxy:
84+
ny = y + dy
85+
nx = x + dx
86+
if not(0 <= ny < h and 0 <= nx < w):
87+
continue
88+
if s[ny][nx] == "#":
89+
continue
90+
if (ny, nx) in visit_path:
91+
continue
92+
visit_path.add((ny, nx))
93+
if is_magnet_neighbour(ny, nx):
94+
continue
95+
q.append((ny, nx))
96+
ans = max(ans, len(visit_path))
97+
print(ans)

atcoder/abc/abc301-400/abc351/e.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
n = int(input())
2+
# 座標変換
3+
convert = lambda x, y: (x + y, x - y)
4+
5+
xy_groups = [[], []]
6+
for _ in range(n):
7+
a, b = map(int, input().split())
8+
xy_groups[(a + b) % 2].append(convert(a, b))
9+
10+
ans = 0
11+
for group_num, xys in enumerate(xy_groups):
12+
xs, ys = [], []
13+
for x, y in xys:
14+
xs.append(x); ys.append(y)
15+
xs.sort(); ys.sort()
16+
sum_x, sum_y = 0, 0
17+
for i in range(len(xys)):
18+
ans += (xs[i] * i - sum_x + ys[i] * i - sum_y) // 2
19+
sum_x += xs[i]; sum_y += ys[i]
20+
print(ans)

atcoder/abc/abc301-400/abc351/f.py

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from collections import defaultdict
2+
from math import gcd
3+
4+
n = int(input())
5+
a = list(map(int, input().split()))
6+
7+
class SegTree:
8+
def __init__(self, n: int, mode: str = "min") -> None:
9+
self.mode = mode
10+
unit_elements = {
11+
"min": 10**13,
12+
"max": -(10**13),
13+
"sum": 0,
14+
"mul": 1,
15+
"gcd": 0,
16+
}
17+
self.e = unit_elements[self.mode] # 単位元
18+
self.tree_size = 2 ** (n - 1).bit_length() # n以上の最小の2のべき乗数
19+
self.tree_value = [self.e] * 2 * self.tree_size
20+
21+
def __str__(self) -> str:
22+
if self.tree_size > 2**4:
23+
return "Segtree size too big"
24+
out = ""
25+
i = 0
26+
j = 0
27+
count = 1
28+
while i < self.tree_size - 1:
29+
if self.tree_value[i] == self.e:
30+
s = "-"
31+
else:
32+
s = str(self.tree_value[i])
33+
s = s.center((self.tree_size * 2) // count, " ")
34+
out += s
35+
i += 1
36+
j += 1
37+
if j == count:
38+
count *= 2
39+
j = 0
40+
out += "\n"
41+
return out
42+
43+
def _op(self, a: int, b: int) -> int:
44+
if self.mode == "min":
45+
return min(a, b)
46+
elif self.mode == "max":
47+
return max(a, b)
48+
elif self.mode == "sum":
49+
return a + b
50+
elif self.mode == "mul":
51+
return a * b
52+
elif self.mode == "gcd":
53+
return gcd(a, b)
54+
55+
raise "no method defined"
56+
57+
def update(self, pos: int, value: int) -> None:
58+
pos += self.tree_size - 1
59+
self.tree_value[pos] = value
60+
while pos:
61+
pos = (pos - 1) // 2
62+
self.tree_value[pos] = self._op(
63+
self.tree_value[pos * 2 + 1], self.tree_value[pos * 2 + 2]
64+
)
65+
66+
def query(self, l: int, r: int) -> int:
67+
r += 1
68+
if r <= l:
69+
return self.e
70+
l += self.tree_size - 1
71+
r += self.tree_size - 2
72+
res = self.e
73+
while r - l > 1:
74+
if l & 1 == 0:
75+
res = self._op(res, self.tree_value[l])
76+
if r & 1 == 1:
77+
res = self._op(res, self.tree_value[r])
78+
r -= 1
79+
l = l // 2
80+
r = (r - 1) // 2
81+
if l == r:
82+
res = self._op(res, self.tree_value[l])
83+
else:
84+
res = self._op(self._op(res, self.tree_value[l]), self.tree_value[r])
85+
return res
86+
87+
# 予め座標圧縮 + セグ木
88+
89+
pos_num = defaultdict(lambda: len(pos_num))
90+
91+
for ai in sorted(a):
92+
pos_num[ai]
93+
94+
sum_tree = SegTree(n + 1, mode="sum")
95+
counter_tree = SegTree(n + 1, mode="sum")
96+
ans = 0
97+
for ai in a:
98+
idx = pos_num[ai]
99+
100+
# それまでの数値とあれこれする
101+
ans += counter_tree.query(0, idx) * ai - sum_tree.query(0, idx)
102+
num = sum_tree.query(idx, idx)
103+
sum_tree.update(idx, num + ai)
104+
cnt = counter_tree.query(idx, idx)
105+
counter_tree.update(idx, cnt + 1)
106+
print(ans)

atcoder/abc/abc301-400/abc351/g.py

Whitespace-only changes.

0 commit comments

Comments
 (0)