Skip to content

Commit cbb7a50

Browse files
committed
abc352
1 parent 41a2505 commit cbb7a50

File tree

7 files changed

+272
-0
lines changed

7 files changed

+272
-0
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
n, x, y, z = map(int, input().split())
2+
3+
from_x = min(x, y)
4+
until_x = max(x, y)
5+
6+
for i in range(from_x, until_x+1):
7+
if i == z:
8+
print("Yes")
9+
exit()
10+
11+
print("No")

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

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import deque
2+
s = list(input())
3+
t = list(input())
4+
5+
s = deque(s)
6+
t = deque(t)
7+
8+
n = len(t)
9+
cnt = 1
10+
ans = []
11+
while len(s) > 0 and len(t) > 0:
12+
if s[0] == t[0]:
13+
s.popleft()
14+
t.popleft()
15+
ans.append(cnt)
16+
else:
17+
t.popleft()
18+
cnt += 1
19+
ans = sorted(ans)
20+
print(*ans, sep=" ")

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
n = int(input())
2+
ab = []
3+
4+
for _ in range(n):
5+
a, b = map(int, input().split())
6+
ab.append((b - a, a, b))
7+
8+
# (頭の大きさ, 肩までの高さ, 頭までの高さ)
9+
10+
ab.sort(reverse=True)
11+
12+
ans = sum([a for _, a, _ in ab[1:]])
13+
ans += ab[0][2]
14+
print(ans)

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

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import heapq
2+
from collections import defaultdict
3+
4+
class MultiSet:
5+
def __init__(self) -> None:
6+
self.cnt_dict = defaultdict(int)
7+
self.rank_min = []
8+
self.rank_max = []
9+
self.size = 0
10+
self.sum = 0
11+
12+
def add(self, num: int) -> None:
13+
cnt = self.cnt_dict.get(num, 0)
14+
self.cnt_dict[num] = cnt + 1
15+
16+
heapq.heappush(self.rank_min, num)
17+
heapq.heappush(self.rank_max, -num)
18+
self.size += 1
19+
self.sum += num
20+
21+
def erase(self, num: int, d: int = 1) -> None:
22+
cnt = self.cnt_dict.get(num, 0)
23+
self.cnt_dict[num] = max(cnt - d, 0)
24+
self.size -= d
25+
self.sum -= num * d
26+
27+
def get_max(self) -> int:
28+
while self.cnt_dict[-self.rank_max[0]] == 0:
29+
-heapq.heappop(self.rank_max)
30+
return -self.rank_max[0]
31+
32+
def get_min(self) -> int:
33+
while self.cnt_dict[self.rank_min[0]] == 0:
34+
heapq.heappop(self.rank_min)
35+
return self.rank_min[0]
36+
37+
def include(self, num: int) -> bool:
38+
return self.cnt_dict.get(num, 0) > 0
39+
40+
n, k = map(int, input().split())
41+
p = list(map(int, input().split()))
42+
43+
positions = dict()
44+
45+
for i, pi in enumerate(p):
46+
positions[pi] = i
47+
48+
multiset = MultiSet()
49+
50+
ans = float("inf")
51+
for i in range(1, k + 1):
52+
multiset.add(positions[i])
53+
54+
ans = multiset.get_max() - multiset.get_min()
55+
56+
for i in range(k + 1, n + 1):
57+
multiset.add(positions[i])
58+
multiset.erase(positions[i - k])
59+
ans = min(ans, multiset.get_max() - multiset.get_min())
60+
print(ans)

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

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# コストが小さい方から全て採用して全域木を作る
2+
n, m = map(int, input().split())
3+
candidates = []
4+
for _ in range(m):
5+
k, weight = map(int, input().split())
6+
a = list(map(int, input().split()))
7+
a = [x - 1 for x in a]
8+
candidates.append((weight, a))
9+
candidates.sort()
10+
11+
class UnionFind():
12+
def __init__(self, n):
13+
self.n = n + 1
14+
self.parents = [-1] * (n + 1)
15+
16+
def find(self, x):
17+
if self.parents[x] < 0:
18+
return x
19+
else:
20+
self.parents[x] = self.find(self.parents[x])
21+
return self.parents[x]
22+
23+
def union(self, x, y):
24+
x = self.find(x)
25+
y = self.find(y)
26+
27+
if x == y:
28+
return
29+
30+
if self.parents[x] > self.parents[y]:
31+
x, y = y, x
32+
33+
self.parents[x] += self.parents[y]
34+
self.parents[y] = x
35+
36+
def size(self, x):
37+
return -self.parents[self.find(x)]
38+
39+
def same(self, x, y):
40+
return self.find(x) == self.find(y)
41+
42+
def members(self, x):
43+
root = self.find(x)
44+
return [i for i in range(self.n) if self.find(i) == root]
45+
46+
def roots(self):
47+
return [i for i, x in enumerate(self.parents) if x < 0]
48+
49+
def group_count(self):
50+
return len(self.roots())
51+
52+
def all_group_members(self):
53+
return {r: self.members(r) for r in self.roots()}
54+
55+
def __str__(self):
56+
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
57+
58+
tree = UnionFind(n)
59+
60+
cost_sum = 0
61+
for weight, a in candidates:
62+
for i in range(1, len(a)):
63+
if not tree.same(a[i - 1], a[i]):
64+
cost_sum += weight
65+
tree.union(a[i - 1], a[i])
66+
else:
67+
continue
68+
69+
if tree.size(0) == n:
70+
print(cost_sum)
71+
else:
72+
print(-1)

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

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from collections import defaultdict
2+
n, m = map(int, input().split())
3+
abc = []
4+
5+
class UnionFind():
6+
def __init__(self, n):
7+
self.n = n + 1
8+
self.parents = [-1] * (n + 1)
9+
10+
def find(self, x):
11+
if self.parents[x] < 0:
12+
return x
13+
else:
14+
self.parents[x] = self.find(self.parents[x])
15+
return self.parents[x]
16+
17+
def union(self, x, y):
18+
x = self.find(x)
19+
y = self.find(y)
20+
21+
if x == y:
22+
return
23+
24+
if self.parents[x] > self.parents[y]:
25+
x, y = y, x
26+
27+
self.parents[x] += self.parents[y]
28+
self.parents[y] = x
29+
30+
def size(self, x):
31+
return -self.parents[self.find(x)]
32+
33+
def same(self, x, y):
34+
return self.find(x) == self.find(y)
35+
36+
def members(self, x):
37+
root = self.find(x)
38+
return [i for i in range(self.n) if self.find(i) == root]
39+
40+
def roots(self):
41+
return [i for i, x in enumerate(self.parents) if x < 0]
42+
43+
def group_count(self):
44+
return len(self.roots())
45+
46+
def all_group_members(self):
47+
return {r: self.members(r) for r in self.roots()}
48+
49+
def __str__(self):
50+
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
51+
52+
groups = UnionFind(n + 1)
53+
num = defaultdict(lambda: 0)
54+
55+
for _ in range(m):
56+
a, b, c = map(int, input().split())
57+
abc.append((a, b, c))
58+
groups.union(a, b)
59+
60+
group_nums = defaultdict(lambda: defaultdict(lambda: 0))
61+
for a, b, c in abc:
62+
group_id = groups.find(a)
63+
if group_nums[group_id][a] != 0:
64+
group_nums[group_id][b] = group_nums[group_id][a] - c
65+
else:
66+
group_nums[group_id][a] = group_nums[group_id][b] + c
67+
68+
candidates = defaultdict(lambda: [])
69+
70+
group_orders = defaultdict(lambda: [])
71+
for group_id, nums in group_nums.items():
72+
min_value = min(nums.values())
73+
sorted_nums = list(sorted([[k, v - min_value] for k, v in nums.items()], key=lambda x: x[1]))
74+
max_num = sorted_nums[-1][1]
75+
for base_num in range(1, n + 1 - max_num):
76+
pattern = []
77+
for pos, value in sorted_nums:
78+
pattern.append((pos, value + base_num))
79+
candidates[group_id].append(pattern)
80+
81+
sets = [set() for _ in range(n + 1)]
82+
83+
for cand in candidates.values():
84+
for c in cand:
85+
for pos, value in c:
86+
sets[pos].add(value)
87+
print(sets)
88+
89+
ans = []
90+
for ss in sets[1:]:
91+
if len(ss) == 1:
92+
ans.append(list(ss)[0])
93+
else:
94+
ans.append(-1)
95+
print(*ans, sep=" ")

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

Whitespace-only changes.

0 commit comments

Comments
 (0)