Skip to content

Commit 7247d82

Browse files
Update as of 24 June 2024
1 parent 83dff68 commit 7247d82

File tree

9 files changed

+587
-276
lines changed

9 files changed

+587
-276
lines changed

README.md

+145-138
Large diffs are not rendered by default.

docs/index.html

+264-138
Large diffs are not rendered by default.

src/As Easy as CAB/easyascab.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import sys; input = sys.stdin.readline; from array import *
2+
3+
class Node:
4+
z = 0
5+
def __init__(self, c, l): self.id = self.z; Node.z += 1; X.append(self); self.ch = {}; self.c = c; self.l = l
6+
7+
# Construct trie
8+
V, N = input().split(); V = ord(V)-96; N = int(N); W = [[ord(x)-97 for x in input().strip()] for _ in range(N)]; G = [set() for _ in range(V)]; indeg, top = [0]*V, []; X = []; T = Node(-1, -1)
9+
for n in range(N):
10+
w = W[n]; t = T
11+
for k, i in enumerate(w):
12+
if i not in t.ch: t.ch[i] = Node(i, k)
13+
t = t.ch[i]
14+
vg = Node.z; g = [set() for _ in range(vg)]; p = []; q = {}
15+
for n in range(N):
16+
w = W[n]; t = T
17+
for i in w: g[t.id].add(t.ch[i].id); g[t.ch[i].id].add(t.id); t = t.ch[i]
18+
p.append(t.id); q[t.id] = n
19+
for i in range(vg): g[i] = [*g[i]]
20+
21+
# Tarjan's OLCA
22+
Q = [[] for _ in range(vg)]; R = []; d = array('i', [0]*vg); par = array('i', range(vg)); d[0] = 1; s = [(0, 0)]; Z = 0
23+
for i in range(N):
24+
for j in range(i): R.append([p[i], p[j], None]), Q[p[i]].append(Z), Q[p[j]].append(Z); Z += 1
25+
def find(i):
26+
if par[i] == i: return i
27+
par[i] = find(par[i]); return par[i]
28+
while s:
29+
ub, p = s.pop(); u = ub//2
30+
if ub%2:
31+
for x in Q[u]:
32+
if R[x][1] == u: R[x][0], R[x][1] = R[x][1], R[x][0]
33+
R[x][2] = find(R[x][1])
34+
par[u] = p
35+
else:
36+
s.append((ub+1, p))
37+
for t in g[u]:
38+
if t != p: d[t] = d[u]+1; s.append((2*t, u))
39+
for a, b, lca in R:
40+
lcp = X[lca].l+1 if lca != vg-1 else 0
41+
if lcp < len(W[q[a]]) and lcp < len(W[q[b]]): G[W[min(q[a], q[b])][lcp]].add(W[max(q[a], q[b])][lcp])
42+
elif lcp == len(W[max(q[a], q[b])]): print('IMPOSSIBLE'), exit(0)
43+
44+
# Topological sort
45+
for v in range(V):
46+
for w in G[v]: indeg[w] += 1
47+
q = [i for i in range(V) if indeg[i] == 0]; r = [set() for _ in range(2*V)]; z = 0
48+
for i in range(V): r[indeg[i]].add(i)
49+
for u in q:
50+
if len(r[0]) > 1: z = 1
51+
top.append(u); r[0].discard(u)
52+
for v in G[u]:
53+
r[indeg[v]].discard(v)
54+
indeg[v] -= 1
55+
r[indeg[v]].add(v)
56+
if indeg[v] == 0: q.append(v)
57+
if len(top) != V: print('IMPOSSIBLE')
58+
elif z: print('AMBIGUOUS')
59+
else: print(''.join(chr(i+97) for i in top))

src/Family DAG/familydag.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sys; input = sys.stdin.readline
2+
R = {}; G = []; X = []; V = ('hillbilly', 'paradox', 'paradox')
3+
while True:
4+
s = input().strip()
5+
if not s: break
6+
if s != 'done':
7+
a, _, b = s.split()
8+
if a not in R: R[a] = len(R); G.append([]); X.append(a)
9+
if b not in R: R[b] = len(R); G.append([]); X.append(b)
10+
G[R[b]].append(R[a])
11+
continue
12+
N = len(R); Z = [0]*N
13+
for i in range(N):
14+
# DFS from i as source, if got multiple visits, gets a report
15+
s = [i]; v = [0]*N
16+
while s:
17+
u = s.pop()
18+
if v[u]: Z[i] |= 1<<(u==i); continue
19+
v[u] = 1; s.extend(G[u])
20+
for i in sorted(R):
21+
if Z[R[i]]: print(i, V[Z[R[i]]-1])
22+
R = {}; G = []; print()

src/Going Dutch/goingdutch.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import sys; input = sys.stdin.readline; from array import *
2+
V, E = map(int, input().split()); M = [0]*V; R = {1<<i: i for i in range(V)}; D = array('i', [-1]*(1<<V)); B = array('i', [0]*(1<<V))
3+
for _ in range(E): a, b, w = map(int, input().split()); M[a] -= w; M[b] += w
4+
for i in range(1, 1<<V): B[i] = B[i-(i&-i)]+M[R[i&-i]]
5+
def dp(bm):
6+
if bm == 0: return 0
7+
if D[bm] != -1: return D[bm]
8+
bm2 = bm; ans = 0
9+
while bm2: nxt = bm2&-bm2; bm2 ^= nxt; ans = max(ans, dp(bm^nxt))
10+
D[bm] = ans+(B[bm]==0); return D[bm]
11+
print(V-dp(2**V-1))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sys; input = sys.stdin.readline; from heapq import *; from array import *
2+
xmin, xmax = map(int, input().split()); C = array('i'); L = []; H = []; D = array('b', [0]*(N:=10**6+1)); M = 10**9+7
3+
for _ in range(int(input())):
4+
t, *r = input().split()
5+
if t < '-':
6+
s, lo, hi = map(int, r)
7+
if lo <= xmin and xmax <= hi: C.append(s)
8+
elif lo <= xmin: heappush(L, (M-hi)*N+s)
9+
elif xmax <= hi: heappush(H, lo*N+s)
10+
else:
11+
D[int(r[0])] = 1 # lazy deletion
12+
while C and D[C[-1]]: C.pop()
13+
while L and D[L[0]%N]: heappop(L)
14+
while H and D[H[0]%N]: heappop(H)
15+
if C: print(1)
16+
elif L and H and L[0]//N+H[0]//N <= M: print(2)
17+
else: print(-1)

src/Target Practice (2)/target.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys; input = sys.stdin.readline; TC = 0
2+
3+
def d2(a, b):
4+
return (a[0]-b[0])**2+(a[1]-b[1])**2
5+
6+
def intersect_check(s1, s2):
7+
(p1, p2), (p3, p4) = s1, s2; (x1, y1), (x2, y2), (x3, y3), (x4, y4) = p1, p2, p3, p4
8+
c1 = (x2-x1)*(y3-y1)-(y2-y1)*(x3-x1); c2 = (x2-x1)*(y4-y1)-(y2-y1)*(x4-x1)
9+
if (c1 < 0 and c2 < 0) or (c1 > 0 and c2 > 0): return 0
10+
c1 = (x4-x3)*(y1-y3)-(y4-y3)*(x1-x3); c2 = (x4-x3)*(y2-y3)-(y4-y3)*(x2-x3)
11+
if (c1 < 0 and c2 < 0) or (c1 > 0 and c2 > 0): return 0
12+
return 1
13+
14+
def pip(p):
15+
ray = (p, (p[0]+10**5, p[1]+1)); return sum(intersect_check(ray, (P[i], P[i+1])) for i in range(n))%2
16+
17+
while (n:=int(input())):
18+
TC += 1; P = [[*map(int, input().split())] for _ in range(n)]; P.append(P[0]); Q = int(input()); print('Case', TC)
19+
for _ in range(Q):
20+
x, y = map(int, input().split()); hit = pip(p:=(x, y)); best = 1e18
21+
for i in range(n):
22+
x1, y1 = P[i]; x2, y2 = P[i+1]; A = y2-y1; B = x1-x2; r = (y-y1)*A-(x-x1)*B; d = A*A+B*B
23+
if r < 0: best = min(best, d2(p, P[i]))
24+
elif r > d: best = min(best, d2(p, P[i+1]))
25+
else: best = min(best, (A*(x-x1)+B*(y-y1))**2/d)
26+
if best == 0: print('Winged!')
27+
elif hit: print('Hit!', best**.5)
28+
else: print('Miss!', best**.5)

src/What's In It For Me/whatsinit.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys; input = sys.stdin.readline; from array import *
2+
3+
def dp(i, p, s):
4+
if s > 100: return 0
5+
if i == N: return s == 100
6+
if D[(x:=10201*i+101*p+s)] != -1: return D[x]
7+
D[x] = 0; lo, hi = divmod(S[i], 101)
8+
for v in range(lo, min(hi, p)+1):
9+
if dp(i+1, v, s+v): D[x] = 1; M[i] = 101*min(M[i]//101, v)+max(M[i]%101, v)
10+
return D[x]
11+
12+
S = []; M = []; E = []; N = int(input()); D = array('b', [-1]*N*101*101)
13+
for i in range(N):
14+
s, p = input().strip().split(); E.append(s)
15+
if p != '?': S.append(102*int(p))
16+
else: S.append(100)
17+
M.append(10100)
18+
dp(0, 100, 0)
19+
for i in range(N): print(E[i], *divmod(M[i], 101))

src/Word Ladder/wordladder2.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
N = int(input()); W = [input().strip() for _ in range(N)]; K = len(W[0]); R = {W[i]:i for i in range(N)}; G = [[] for _ in range(N)]
2+
for i in range(N):
3+
for j in range(K):
4+
for k in range(65, 91):
5+
if chr(k) != W[i][j]:
6+
s = W[i][:j]+chr(k)+W[i][j+1:]
7+
if s not in R: R[s] = len(R); W.append(s); G.append([])
8+
G[R[W[i]]].append(R[s]); G[R[s]].append(R[W[i]])
9+
D = [-1]*len(G); E = [-1]*len(G); Q = [0]; D[0] = 0; R = [1]; E[1] = 0
10+
for u in Q:
11+
for v in G[u]:
12+
if D[v] == -1: D[v] = D[u]+1; Q.append(v) if v < N else 0
13+
for u in R:
14+
for v in G[u]:
15+
if E[v] == -1: E[v] = E[u]+1; R.append(v) if v < N else 0
16+
ans = None; dist = D[1] if D[1] > -1 else 2000
17+
for i in range(N, len(G)):
18+
if D[i] > -1 < E[i]:
19+
if D[i]+E[i] < dist: ans = W[i]; dist = D[i]+E[i]
20+
elif D[i]+E[i] == dist and ans != None: ans = min(W[i], ans)
21+
if ans == None: print(0, dist if dist < 2000 else -1)
22+
else: print(ans, dist)

0 commit comments

Comments
 (0)