SX, SY, GX, GY = map(int, input().split())
dx = GX - SX
dy = GY + SY
ret = SY / dy * dx + SX
print(ret)
C
8の階乗は40320です
全探索でOK
python
def solve(N, K, dist):
from itertools import permutations
ret = 0
for order in permutations(range(1, N)):
d = 0
prev = 0
for x in order:
d += dist[prev][x]
prev = x
d += dist[prev][0]
if d == K:
ret += 1
return ret
N, W = map(int, input().split())
diff = [0] * 20_0010
for _i in range(N):
S, T, P = map(int, input().split())
diff[S] += P
diff[T] -= P
usage = 0
for v in diff:
usage += v
if usage > W:
print("No")
return
print("Yes")
def solve(H, W, data):
MOD = 1_000_000_007
N = len(data)
table = [0] * N
h_accum = [0] * N
v_accum = [0] * N
d_accum = [0] * N
table[WIDTH + 1] = 1
h_accum[WIDTH + 1] = 1
v_accum[WIDTH + 1] = 1
d_accum[WIDTH + 1] = 1
for pos in allPosition():
if pos == WIDTH + 1:
continue
if data[pos] == 0:
table[pos] = 0
h_accum[pos] = 0
v_accum[pos] = 0
d_accum[pos] = 0
continue
h_value = h_accum[pos - 1]
v_value = v_accum[pos - WIDTH]
d_value = d_accum[pos - WIDTH - 1]
ret = h_value + v_value + d_value
ret %= MOD
table[pos] = ret
h_accum[pos] = (h_accum[pos - 1] + ret) % MOD
v_accum[pos] = (v_accum[pos - WIDTH] + ret) % MOD
d_accum[pos] = (d_accum[pos - WIDTH - 1] + ret) % MOD
return ret
F
考えたこと
クラスの種類は20万ありえる
UnionFindでできなさそう?
対数オーダーでできないか?
UnionFindでやる場合、代表元にクラスごとの個数を持たせる
「クラスxが何人」という値を素朴に配列で持つとO(N)
読み出しは定数オーダー
これを対数オーダーまで落として良いので、代わりにマージを高速にしたい
読み出しを対数オーダーにする→二分探索
ソート済み配列として持ったらどうなる?
マージは内容物の量の線形オーダー
内容物のオーダーでいいならハッシュテーブルで良いのでは
キーがないことがあるのでdefaultdict、いや、個数カウントだからCounterか
一旦それで送信してみて他のバグがないか確認してみよう→意外とあっさりAC
実装
UnionFindライブラリに手を入れてマージ時にCounterを更新する
python
def unite(x, y):
x = find_root(x)
y = find_root(y)
if x == y:
return # already united
if rank[x] < rank[y]:
parent[x] = y
cls[y].update(cls[x]) # ***
else:
parent[y] = x
if rank[x] == rank[y]:
rank[x] += 1
cls[x].update(cls[y]) # ***
本体
python
def main():
global cls
from collections import Counter
N, Q = map(int, input().split())
init_unionfind(N)
CS = list(map(int, input().split()))
cls = [Counter([CS[i] - 1]) for i in range(N)]
for _q in range(Q):
typ, a, b = map(int, input().split())
if typ == 1:
unite(a - 1, b - 1)
else:
root = find_root(a - 1)
print(cls[root].get(b - 1, 0))