Skip to content

Commit 7c41f0b

Browse files
committed
abc350
1 parent f51a14d commit 7c41f0b

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
abc = input()
2+
abc_sets = set()
3+
4+
for i in range(1, 350):
5+
if i == 316:
6+
continue
7+
num_str = str(i)
8+
num_str = "0" * (3 - len(num_str)) + num_str
9+
abc_sets.add("ABC" + num_str)
10+
11+
if abc in abc_sets:
12+
print("Yes")
13+
else:
14+
print("No")

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

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
n, q = map(int, input().split())
2+
t = list(map(int, input().split()))
3+
4+
teeth = [True] * n
5+
6+
for ti in t:
7+
if teeth[ti - 1]:
8+
teeth[ti - 1] = False
9+
else:
10+
teeth[ti - 1] = True
11+
12+
print(sum(teeth))

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

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
n = int(input())
2+
a = list(map(int, input().split()))
3+
4+
positions = dict()
5+
6+
for i in range(n):
7+
positions[a[i]] = i
8+
9+
ans = []
10+
11+
for i in range(1, n + 1):
12+
if a[i - 1] == i:
13+
continue
14+
else:
15+
# swap
16+
ans.append(f"{i} {positions[i] + 1}")
17+
a[positions[i]] = a[i - 1]
18+
positions[a[i - 1]] = positions[i]
19+
positions[i] = i - 1
20+
a[i - 1] = i
21+
print(len(ans))
22+
if len(ans) > 0:
23+
print(*ans, sep="\n")

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

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from collections import defaultdict
2+
3+
n, m = map(int, input().split())
4+
ab = []
5+
for _ in range(m):
6+
a, b = map(int, input().split())
7+
ab.append((a - 1, b - 1))
8+
9+
class UnionFind():
10+
def __init__(self, n):
11+
self.n = n + 1
12+
self.parents = [-1] * (n + 1)
13+
14+
def find(self, x):
15+
if self.parents[x] < 0:
16+
return x
17+
else:
18+
self.parents[x] = self.find(self.parents[x])
19+
return self.parents[x]
20+
21+
def union(self, x, y):
22+
x = self.find(x)
23+
y = self.find(y)
24+
25+
if x == y:
26+
return
27+
28+
if self.parents[x] > self.parents[y]:
29+
x, y = y, x
30+
31+
self.parents[x] += self.parents[y]
32+
self.parents[y] = x
33+
34+
def size(self, x):
35+
return -self.parents[self.find(x)]
36+
37+
def same(self, x, y):
38+
return self.find(x) == self.find(y)
39+
40+
def members(self, x):
41+
root = self.find(x)
42+
return [i for i in range(self.n) if self.find(i) == root]
43+
44+
def roots(self):
45+
return [i for i, x in enumerate(self.parents) if x < 0]
46+
47+
def group_count(self):
48+
return len(self.roots())
49+
50+
def all_group_members(self):
51+
return {r: self.members(r) for r in self.roots()}
52+
53+
def __str__(self):
54+
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
55+
56+
uf_tree = UnionFind(n=n)
57+
58+
for a, b in ab:
59+
uf_tree.union(a, b)
60+
61+
group_counts = defaultdict(int)
62+
63+
for i in range(n):
64+
group = uf_tree.find(i)
65+
group_counts[group] += 1
66+
67+
path_count = defaultdict(int)
68+
for a, _ in ab:
69+
group = uf_tree.find(a)
70+
path_count[group] += 1
71+
72+
ans = 0
73+
for group in group_counts.keys():
74+
ans += group_counts[group] * (group_counts[group] - 1) // 2
75+
ans -= path_count[group]
76+
77+
print(ans)

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

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from collections import defaultdict
2+
3+
n, a, x, y = map(int, input().split())
4+
size = 5
5+
dp = [defaultdict(int) for _ in range(size)]
6+
7+
dp[0][n] = 0
8+
for i in range(1, size):
9+
keys = dp[i - 1].keys()
10+
for key in keys:
11+
dp[i][key] = dp[i - 1][key]
12+
dp[i][key // a] += x
13+
for j in range(1, 7):
14+
dp[i][key // j] += (y / 6)
15+
16+
print(dp)

0 commit comments

Comments
 (0)