Skip to content

Commit 963915e

Browse files
committed
abc342
1 parent 14e261a commit 963915e

File tree

7 files changed

+189
-0
lines changed

7 files changed

+189
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from collections import defaultdict
2+
3+
s = list(input())
4+
counter = defaultdict(lambda: 0)
5+
for si in s:
6+
counter[si] += 1
7+
8+
for i in range(1, len(s) + 1):
9+
if counter[s[i - 1]] == 1:
10+
print(i)
11+
exit()

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from collections import defaultdict
2+
3+
n = int(input())
4+
p_map = defaultdict(lambda: -1)
5+
p = list(map(int, input().split()))
6+
for i in range(n):
7+
p_map[p[i]] = i + 1
8+
9+
q = int(input())
10+
ans = []
11+
for _ in range(q):
12+
a, b = map(int, input().split())
13+
if p_map[a] < p_map[b]:
14+
ans.append(a)
15+
else:
16+
ans.append(b)
17+
18+
print(*ans, sep="\n")

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from collections import defaultdict
2+
3+
n = int(input())
4+
s = list(input())
5+
q = int(input())
6+
cd = []
7+
mapping_rules = defaultdict(lambda: "")
8+
for i in range(26):
9+
mapping_rules[chr(ord("a") + i)] = chr(ord("a") + i)
10+
11+
for _ in range(q):
12+
c, d = input().split()
13+
for i in range(26):
14+
if mapping_rules[chr(ord("a") + i)] == c:
15+
mapping_rules[chr(ord("a") + i)] = d
16+
17+
def mapping(s, mapping_rules):
18+
return "".join([mapping_rules[si] for si in s])
19+
20+
ans = mapping(s, mapping_rules)
21+
print(ans)

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import List
2+
from functools import lru_cache
3+
from collections import defaultdict
4+
5+
n = int(input())
6+
a = list(map(int, input().split()))
7+
8+
@lru_cache(maxsize=None)
9+
def prime_factorize(n: int) -> List[int]:
10+
# 素因数分解
11+
# Return list of prime factorized result
12+
# O(sqrt(n))
13+
a = []
14+
while n % 2 == 0:
15+
a.append(2)
16+
n //= 2
17+
f = 3
18+
while f * f <= n:
19+
if n % f == 0:
20+
a.append(f)
21+
n //= f
22+
else:
23+
f += 2
24+
if n != 1:
25+
a.append(n)
26+
return a
27+
28+
counter = defaultdict(lambda: 0)
29+
30+
for ai in a:
31+
counter_i = defaultdict(lambda: 0)
32+
if ai == 0:
33+
counter["0"] += 1
34+
continue
35+
36+
for prime in prime_factorize(ai):
37+
counter_i[prime] += 1
38+
counter_i[prime] %= 2
39+
40+
str_list = []
41+
for k, v in counter_i.items():
42+
if v == 1:
43+
str_list.append(str(k))
44+
counter[" ".join(sorted(str_list))] += 1
45+
46+
ans = 0
47+
for k, v in counter.items():
48+
ans += v * (v - 1) // 2
49+
50+
ans += counter["0"] * (n - counter["0"])
51+
52+
print(ans)
53+
54+

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from collections import defaultdict, deque
2+
import sys
3+
import heapq
4+
5+
input = lambda: sys.stdin.readline().rstrip()
6+
sys.setrecursionlimit(20000000)
7+
INF = float("inf")
8+
9+
n, m = map(int, input().split())
10+
to_path = defaultdict(lambda: [])
11+
for _ in range(m):
12+
l, d, k, c, a, b = map(int, input().split())
13+
to_path[b].append((l, d, k, c, a, b))
14+
15+
def calc_max_ts(l, d, k, c, a, b, ts):
16+
# ts: 駅bに到着しているべき時刻
17+
# return: 駅aに到着している時刻
18+
if ts < l + c:
19+
# 終電に間に合う電車が存在しない
20+
return -INF
21+
elif ts >= l + (k - 1) * d + c:
22+
# 最も遅い電車で良い
23+
return l + (k - 1) * d
24+
else:
25+
# 適切なkを探す必要がある
26+
ki = (l + (k - 1) * d + c) - ts
27+
if ki % d == 0:
28+
ki //= d
29+
else:
30+
ki = ki // d + 1
31+
return l + (k - 1 - ki) * d
32+
33+
# Nから逆に辿っていく
34+
# その駅の終電時刻: max_ts
35+
max_ts = defaultdict(lambda: -INF)
36+
q = []
37+
heapq.heapify(q)
38+
for l, d, k, c, a, b in to_path[n]:
39+
max_ts[b] = max(max_ts[b], l + (k - 1) * d + c)
40+
heapq.heappush(q, [-max_ts[b], b])
41+
42+
visited = set([])
43+
while q:
44+
current_station = heapq.heappop(q)[1]
45+
if current_station in visited:
46+
continue
47+
visited.add(current_station)
48+
for l, d, k, c, a, b in to_path[current_station]:
49+
# 選択肢の中から最も遅い電車を選択する
50+
tsi = calc_max_ts(l, d, k, c, a, b, max_ts[b])
51+
max_ts[a] = max(max_ts[a], tsi)
52+
if a not in visited and max_ts[a] != -INF:
53+
heapq.heappush(q, [-max_ts[a], a])
54+
55+
ans = []
56+
for i in range(1, n):
57+
if max_ts[i] == -INF:
58+
ans.append("Unreachable")
59+
else:
60+
ans.append(max_ts[i])
61+
print(*ans, sep="\n")

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
n, l, d = map(int, input().split())
2+
3+
# ブラックジャックの勝率をDPする
4+
# dp[i][j]: プレイヤーのスコアがi、ディーラーのスコアがjの時の勝率
5+
# 逆からDPする
6+
7+
dealer_dp = [0] * (n + d + 1)
8+
player_dp = [0] * (n + 1)
9+
dealer_dp[0] = 1.0
10+
11+
# dp[i]: ディーラーのスコアがiになる確率
12+
for i in range(l):
13+
for j in range(i + 1, i + 1 + d):
14+
dealer_dp[j] += dealer_dp[i] / d
15+
dealer_dp[i] = 0.0
16+
17+
for i in range(n):
18+
dealer_win_prob = 0.0
19+
for j in range(i + 1, n):
20+
dealer_win_prob += dealer_dp[j]
21+
player_dp[i] = 1.0 - dealer_win_prob
22+
23+
print(dealer_dp)
24+
print(player_dp)

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

Whitespace-only changes.

0 commit comments

Comments
 (0)