Skip to content

Commit

Permalink
Update as of 30 April 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellDash332 committed Apr 29, 2024
1 parent 765f198 commit 3aff2a1
Show file tree
Hide file tree
Showing 11 changed files with 743 additions and 248 deletions.
261 changes: 137 additions & 124 deletions README.md

Large diffs are not rendered by default.

485 changes: 361 additions & 124 deletions docs/index.html

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions src/Bicikli/bicikli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys; input = sys.stdin.readline
n, m = map(int, input().split()); n += 1; g = [{} for _ in range(n)]; dp = [0]*n; high = 0; I = [0]*n; v = [0]*n
for _ in range(m):
a, b = map(int, input().split())
if b not in g[a]: g[a][b] = 0
g[a][b] += 1
q = [1]; dp[2] = 1; T = []
for u in q:
if v[u]: continue
v[u] = 1; q.extend(g[u])
for i in range(n):
if v[i]:
for j in g[i]:
if v[j]: I[j] += 1
tq = [i for i in range(n) if I[i] == 0 and v[i]]
for u in tq:
T.append(u)
for x in g[u]:
I[x] -= 1
if I[x] == 0 and v[x]: tq.append(x)
if 2 not in T: print('inf'), exit(0)
while T:
u = T.pop()
for x in g[u]:
dp[u] += g[u][x]*dp[x]
if dp[u] >= 10**9: high = 1
if high: dp[u] %= 10**9
print(dp[1] if not high else str(dp[1]).zfill(9))
24 changes: 24 additions & 0 deletions src/Cuboid Slicing Game/cuboidslicinggame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from array import *
dp = array('i', [-1]*30000); a = array('b', [0]*30000)

def mex(s):
for i in s: a[i] = 1
for i in range(max(s)+2):
if a[i] == 0:
for j in s: a[j] = 0
return i

# Sprague-Grundy
def win(x, y, z):
if x < 1 or y < 1 or z < 1: return 0
if dp[(t:=961*x+31*y+z)] != -1: return dp[t]
s = []
# for every cut combo XOR across all 8 cuboids
for i in range(x//2+1):
for j in range(y//2+1):
for k in range(z//2+1): s.append(win(i, j, k)^win(i, j, z-k-1)^win(i, y-j-1, k)^win(i, y-j-1, z-k-1)^win(x-i-1, j, k)^win(x-i-1, j, z-k-1)^win(x-i-1, y-j-1, k)^win(x-i-1, y-j-1, z-k-1))
dp[t] = mex(s); return dp[t]

p = input(); g = 0; n = int(input())
for _ in range(n): g ^= win(*map(int, input().split()))
print(['RUBEN', 'ALBERT'][(p=='ALBERT')^(g==0)])
4 changes: 4 additions & 0 deletions src/Cuboid Slicing Game/cuboidslicinggame_hc.py

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions src/Holiday Stars/holidaystars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from math import *

def centroid(p):
a = cx = cy = 0; n = len(p)
for i in range(n): d = p[i][0]*p[(i+1)%n][1]-p[(i+1)%n][0]*p[i][1]; a += d; cx += (p[i][0]+p[(i+1)%n][0])*d; cy += (p[i][1]+p[(i+1)%n][1])*d
return (cx/a/3, cy/a/3)

n, omega, v0, theta, w = map(float, input().split()); n = round(n); theta *= pi/180; poly = []; vx = v0*cos(theta)
for _ in range(n): poly.append([*map(float, input().split())])
cx, cy = centroid(poly)
a = [hypot(p[0]-cx, p[1]-cy) for p in poly]; d = [atan2(p[0]-cx, p[1]-cy) for p in poly]; t = []

def f(i, t):
return cx+vx*t+a[i]*sin(omega*t+d[i])
def df(i, t):
return vx+a[i]*omega*cos(omega*t+d[i])
def d2f(i, t):
return -a[i]*omega*omega*sin(omega*t+d[i])

for i in range(n):
if vx-a[i]*omega > -1e-9:
# binary search on monotic increasing fn
lo, hi = 0, 1e9
while abs(lo-hi)>1e-6:
mi = (lo+hi)/2
if f(i, mi) < w: lo = mi
else: hi = mi
t.append(mi)
else:
# find points such that df(i, t) == 0
# cos(omega*t+d[i]) == -vx/(a[i]*omega)
# -> omega*t+d[i] = +-acos(-vx/a[i]/omega)+2*pi*k
# -> t = (2*pi*k-d[i]+-acos(-vx/a[i]/omega))/omega
t1 = (-d[i]+acos(-vx/a[i]/omega))/omega
while t1 < 0: t1 += 2*pi/omega
t2 = (-d[i]-acos(-vx/a[i]/omega))/omega
while t2 < 0: t2 += 2*pi/omega
if t1 > t2: t1, t2 = t2, t1
# check if f(i, t) goes downwards as t = t1
# if so, [0, t1] is uw, then [t1, t2] is dw, then [t2, t1+2*pi/omega] uw, and so on...
if d2f(i, t1) > 0: lo, hi = t1, t2
else: lo, hi = t2-2*pi/omega, t1
k = max(ceil((w-f(i, hi))/vx/2/pi*omega), 0); w2 = w-vx*2*pi/omega*k
while abs(lo-hi)>1e-6:
mi = (lo+hi)/2
if f(i, mi) < w2: lo = mi
else: hi = mi
t.append(mi+2*pi/omega*k)
v = min(range(n), key=lambda x: t[x]); print(v+1, t[v])
83 changes: 83 additions & 0 deletions src/Lista/lista.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
def lis(arr):
def upper_bound(sub, idx):
lo, hi = 0, len(sub) - 1
while hi > lo:
mid = (lo + hi) // 2
if arr[sub[mid]] < arr[idx]: lo = mid + 1
else: hi = mid
return hi
temp = []; par = [None]*len(arr)
for i in range(len(arr)):
if not temp or arr[i] > arr[temp[-1]]:
if temp: par[i] = temp[-1]
temp.append(i)
else:
rep = upper_bound(temp, i); temp[rep] = i
if rep != 0: par[i] = temp[rep - 1]
final = []; curr = temp[-1]
while curr != None: final.append(arr[curr]); curr = par[curr]
return final[::-1]

class Node:
def __init__(self, val):
self.val = val
self.next = self.prev = None

class LL:
def __init__(self):
self.tail = None
self.size = 0
def insert(self, node):
if self.tail:
if self.tail.next:
self.tail.next.prev = node
node.next = self.tail.next
self.tail.next = node
node.prev = self.tail
else:
node.prev = node.next = node
self.tail = node
self.size += 1
def insert_at(self, node, at):
prev, curr, succ = at, node, at.next
prev.next, curr.prev, curr.next, succ.prev = curr, prev, succ, curr
if self.tail == prev:
self.tail = curr
self.size += 1
def remove(self, node):
if node.prev: node.prev.next = node.next
if node.next: node.next.prev = node.prev
if self.tail == node: self.tail = node.prev
node.next, node.prev = None, None
self.size -= 1
return node
def print(self):
arr = []
curr = self.tail
for _ in range(self.size):
arr.append(curr.val)
curr = curr.prev
return arr[::-1]

import sys; input = sys.stdin.readline
from bisect import *
n, m = map(int, input().split())
h = {i:Node(i) for i in range(n)}
ll = LL()
for i in range(n): ll.insert(h[i])
ll.print()
for _ in range(m):
c, a, b = input().split(); a = int(a)-1; b = int(b)-1
if c == 'A':
fr = h[b].prev == ll.tail
ll.insert_at(ll.remove(h[a]), h[b].prev)
if fr: ll.tail = h[a].prev
else: ll.insert_at(ll.remove(h[a]), h[b])
A = ll.print(); L = lis(A); S = [0]*n; eol = L[-1]
print(n-len(L))
for i in L: S[i] = 1
for i in range(n):
if S[i] == 0:
p = bisect_right(L, i)
if p == len(L): print('B', i+1, eol+1); eol = i
else: print('A', i+1, L[p]+1)
22 changes: 22 additions & 0 deletions src/Pedal Power/pedalpower.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sys; input = sys.stdin.readline
V = int(input()); I = float('inf'); b = [[I]*V for _ in range(V)]; nb = [[I]*V for _ in range(V)]
for i in range(V): b[i][i] = nb[i][i] = 0
for _ in range(int(input())): x, y, w = map(int, input().split()); b[x][y] = b[y][x] = w
for k in range(V):
for i in range(V):
for j in range(V): b[i][j] = min(b[i][j], b[i][k]+b[k][j])
for _ in range(int(input())): x, y, w = map(int, input().split()); nb[x][y] = nb[y][x] = w
for k in range(V):
for i in range(V):
for j in range(V): nb[i][j] = min(nb[i][j], nb[i][k]+nb[k][j])
q = int(input()); a = [0, *map(int, input().split()), 0]; dp = [I]*V; dp[0] = 0
for i in range(q+1):
ss, ee = a[i], a[i+1]; tmp = [I]*V
for j in range(V):
# bike currently at j, want to go from ss to ee
for k in range(V):
# move bike from j to k?
if j == k: tmp[k] = min(tmp[k], dp[j]+nb[ss][ee]) # no need to move bike
else: tmp[k] = min(tmp[k], dp[j]+nb[ss][j]+b[j][k]+nb[k][ee]) # go to j, move bike to k, go to ee
dp = tmp
print(dp[0])
12 changes: 12 additions & 0 deletions src/Secret Santa Cycles/secretsantacycles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sys; input = sys.stdin.readline
V = int(input()); I = [0]*V; G = [[] for _ in range(V)]; S = [0]*V; A = 0; C = []
for i in range(V): j = int(input())-1; I[j] += 1; G[i].append(j); G[j].append(i)
for i in range(V):
if S[i] == 0:
q = [i]; c = []
for u in q:
if S[u]: continue
c.append(u); S[u] = 1; q.extend(G[u])
C.append(c)
for c in C: A += max(sum(max(I[j]-1, 0) for j in c), len(C)>1)
print(A)
18 changes: 18 additions & 0 deletions src/Tenkici/tenkici.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
n = int(input()); R = []; C = []
for i in range(n): r, c = map(int, input().split()); t = [r, c, i]; R.append(t); C.append(t)
R.sort(); C.sort(key=lambda x: (x[1], x[0])); rw = [0]*n
print(sum(abs(R[i][0]-i-1)+abs(C[i][1]-i-1) for i in range(n)))
for i in range(n):
if C[i][1] < i+1: rw[C[i][2]] = 1
for i in range(n-1, -1, -1):
if rw[C[i][2]]:
for _ in range(i+1-C[i][1]): print(C[i][2]+1, 'R'); C[i][1] += 1
for i in range(n):
if not rw[C[i][2]]:
for _ in range(C[i][1]-i-1): print(C[i][2]+1, 'L'); C[i][1] -= 1
for i in range(n):
d = R[i][0]-i-1
if d > 0:
for _ in range(d): print(R[i][2]+1, 'U'); R[i][0] -= 1
else:
for _ in range(-d): print(R[i][2]+1, 'D'); R[i][0] += 1
5 changes: 5 additions & 0 deletions src/Veggja Kalli/veggjakalli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from array import *
n, m = map(int, input().split()); a = array('b', map(lambda x: x=='#', input())); s = 0; p = array('i', [s:=s+i for i in a]); ans = 1e18
for i in range(n-m-1):
if a[i]*a[i+m+1] and ans > p[i+m]-p[i]: ans = p[i+m]-p[i]
print(ans if ans < 1e18 else 'Neibb')

0 comments on commit 3aff2a1

Please sign in to comment.