-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from collections import defaultdict | ||
|
||
s = list(input()) | ||
counter = defaultdict(lambda: 0) | ||
for si in s: | ||
counter[si] += 1 | ||
|
||
for i in range(1, len(s) + 1): | ||
if counter[s[i - 1]] == 1: | ||
print(i) | ||
exit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from collections import defaultdict | ||
|
||
n = int(input()) | ||
p_map = defaultdict(lambda: -1) | ||
p = list(map(int, input().split())) | ||
for i in range(n): | ||
p_map[p[i]] = i + 1 | ||
|
||
q = int(input()) | ||
ans = [] | ||
for _ in range(q): | ||
a, b = map(int, input().split()) | ||
if p_map[a] < p_map[b]: | ||
ans.append(a) | ||
else: | ||
ans.append(b) | ||
|
||
print(*ans, sep="\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from collections import defaultdict | ||
|
||
n = int(input()) | ||
s = list(input()) | ||
q = int(input()) | ||
cd = [] | ||
mapping_rules = defaultdict(lambda: "") | ||
for i in range(26): | ||
mapping_rules[chr(ord("a") + i)] = chr(ord("a") + i) | ||
|
||
for _ in range(q): | ||
c, d = input().split() | ||
for i in range(26): | ||
if mapping_rules[chr(ord("a") + i)] == c: | ||
mapping_rules[chr(ord("a") + i)] = d | ||
|
||
def mapping(s, mapping_rules): | ||
return "".join([mapping_rules[si] for si in s]) | ||
|
||
ans = mapping(s, mapping_rules) | ||
print(ans) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import List | ||
from functools import lru_cache | ||
from collections import defaultdict | ||
|
||
n = int(input()) | ||
a = list(map(int, input().split())) | ||
|
||
@lru_cache(maxsize=None) | ||
def prime_factorize(n: int) -> List[int]: | ||
# 素因数分解 | ||
# Return list of prime factorized result | ||
# O(sqrt(n)) | ||
a = [] | ||
while n % 2 == 0: | ||
a.append(2) | ||
n //= 2 | ||
f = 3 | ||
while f * f <= n: | ||
if n % f == 0: | ||
a.append(f) | ||
n //= f | ||
else: | ||
f += 2 | ||
if n != 1: | ||
a.append(n) | ||
return a | ||
|
||
counter = defaultdict(lambda: 0) | ||
|
||
for ai in a: | ||
counter_i = defaultdict(lambda: 0) | ||
if ai == 0: | ||
counter["0"] += 1 | ||
continue | ||
|
||
for prime in prime_factorize(ai): | ||
counter_i[prime] += 1 | ||
counter_i[prime] %= 2 | ||
|
||
str_list = [] | ||
for k, v in counter_i.items(): | ||
if v == 1: | ||
str_list.append(str(k)) | ||
counter[" ".join(sorted(str_list))] += 1 | ||
|
||
ans = 0 | ||
for k, v in counter.items(): | ||
ans += v * (v - 1) // 2 | ||
|
||
ans += counter["0"] * (n - counter["0"]) | ||
|
||
print(ans) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from collections import defaultdict, deque | ||
import sys | ||
import heapq | ||
|
||
input = lambda: sys.stdin.readline().rstrip() | ||
sys.setrecursionlimit(20000000) | ||
INF = float("inf") | ||
|
||
n, m = map(int, input().split()) | ||
to_path = defaultdict(lambda: []) | ||
for _ in range(m): | ||
l, d, k, c, a, b = map(int, input().split()) | ||
to_path[b].append((l, d, k, c, a, b)) | ||
|
||
def calc_max_ts(l, d, k, c, a, b, ts): | ||
# ts: 駅bに到着しているべき時刻 | ||
# return: 駅aに到着している時刻 | ||
if ts < l + c: | ||
# 終電に間に合う電車が存在しない | ||
return -INF | ||
elif ts >= l + (k - 1) * d + c: | ||
# 最も遅い電車で良い | ||
return l + (k - 1) * d | ||
else: | ||
# 適切なkを探す必要がある | ||
ki = (l + (k - 1) * d + c) - ts | ||
if ki % d == 0: | ||
ki //= d | ||
else: | ||
ki = ki // d + 1 | ||
return l + (k - 1 - ki) * d | ||
|
||
# Nから逆に辿っていく | ||
# その駅の終電時刻: max_ts | ||
max_ts = defaultdict(lambda: -INF) | ||
q = [] | ||
heapq.heapify(q) | ||
for l, d, k, c, a, b in to_path[n]: | ||
max_ts[b] = max(max_ts[b], l + (k - 1) * d + c) | ||
heapq.heappush(q, [-max_ts[b], b]) | ||
|
||
visited = set([]) | ||
while q: | ||
current_station = heapq.heappop(q)[1] | ||
if current_station in visited: | ||
continue | ||
visited.add(current_station) | ||
for l, d, k, c, a, b in to_path[current_station]: | ||
# 選択肢の中から最も遅い電車を選択する | ||
tsi = calc_max_ts(l, d, k, c, a, b, max_ts[b]) | ||
max_ts[a] = max(max_ts[a], tsi) | ||
if a not in visited and max_ts[a] != -INF: | ||
heapq.heappush(q, [-max_ts[a], a]) | ||
|
||
ans = [] | ||
for i in range(1, n): | ||
if max_ts[i] == -INF: | ||
ans.append("Unreachable") | ||
else: | ||
ans.append(max_ts[i]) | ||
print(*ans, sep="\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
n, l, d = map(int, input().split()) | ||
|
||
# ブラックジャックの勝率をDPする | ||
# dp[i][j]: プレイヤーのスコアがi、ディーラーのスコアがjの時の勝率 | ||
# 逆からDPする | ||
|
||
dealer_dp = [0] * (n + d + 1) | ||
player_dp = [0] * (n + 1) | ||
dealer_dp[0] = 1.0 | ||
|
||
# dp[i]: ディーラーのスコアがiになる確率 | ||
for i in range(l): | ||
for j in range(i + 1, i + 1 + d): | ||
dealer_dp[j] += dealer_dp[i] / d | ||
dealer_dp[i] = 0.0 | ||
|
||
for i in range(n): | ||
dealer_win_prob = 0.0 | ||
for j in range(i + 1, n): | ||
dealer_win_prob += dealer_dp[j] | ||
player_dp[i] = 1.0 - dealer_win_prob | ||
|
||
print(dealer_dp) | ||
print(player_dp) |
Empty file.