Skip to content

Commit

Permalink
Update as of 19 January 2025
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellDash332 committed Jan 18, 2025
1 parent bdeb2c1 commit fff2497
Show file tree
Hide file tree
Showing 15 changed files with 1,625 additions and 704 deletions.
724 changes: 372 additions & 352 deletions README.md

Large diffs are not rendered by default.

1,067 changes: 715 additions & 352 deletions docs/index.html

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/Apparatus/apparatus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from collections import *
n, m = map(int, input().split()); f = [1]; M = 1000003; z = 1
for i in range(1, 1002): f.append(f[-1]*i%M)
a = ['']*n; b = ['']*n
for _ in range(m):
s = input(); t = input()
for i in range(n): a[i] += s[i]; b[i] += t[i]
c = Counter(a)
if c != Counter(b): print(0), exit(0)
for i in c.values(): z = z*f[i]%M
print(z)
37 changes: 37 additions & 0 deletions src/Bowlstack/bowlstack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from itertools import *

def f(h, r, R, hp, rp, Rp):
if R <= rp:
# completely on top of the other
return 1, h
elif Rp <= R and hp/(Rp-rp) <= h/(R-r) and hp <= (Rp-r)*h/(R-r):
# similar to case 2, bowl 1 and bowl 2
return 2, (Rp-r)*h/(R-r)-hp
elif r <= rp <= R and hp/(Rp-rp) >= h/(R-r):
# similar to case 1
return 3, (rp-r)*h/(R-r)
elif Rp >= R >= rp and hp/(Rp-rp) <= h/(R-r) and hp*(R-rp)/(Rp-rp) <= h:
# similar to case 2, bowl 2 and bowl 3
return 4, h-hp+(Rp-R)*hp/(Rp-rp)
elif rp <= r:
# top bowl touches the bottom bowl's base
return 5, 0
else:
# should not happen
assert 0, (h, r, R, hp, rp, Rp)

for _ in range(int(input())):
N = int(input()); Q = [[*map(int, input().split())] for _ in range(N)]; B = 1e9
for P in permutations(Q):
H = [0]*N; M = 0
for i in range(N):
# consider P[i] as the top of the stack
hp, rp, Rp = P[i]
for j in range(i):
h, r, R = P[j]
_, dh = f(h, r, R, hp, rp, Rp)
H[i] = max(H[i], H[j]+dh)
M = max(M, H[i]+hp)
if M > B: break
B = min(B, int(M))
print(B)
16 changes: 16 additions & 0 deletions src/Dorm Room Divide/dormroomdivide.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def area(p):
a, n = 0, len(p)
for i in range(n): a += p[i][0]*p[(i+1)%n][1] - p[i][1]*p[(i+1)%n][0]
return abs(a)/2
N = int(input())
P = [[*map(int, input().split())] for _ in range(N)]
A = [area((P[0], P[i], P[i+1])) for i in range(1, N-1)]
Z = sum(A)
U = 0
for i in range(1, N-1):
if 2*(U+A[i-1]) <= Z: U += A[i-1]
else:
t = 1-(Z/2-U)/A[i-1]
x = t*P[i][0]+(1-t)*P[i+1][0]
y = t*P[i][1]+(1-t)*P[i+1][1]
print(x, y), exit(0)
14 changes: 14 additions & 0 deletions src/Dream Team/dreamteam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
N, P, Q = map(int, input().split())
A = [*map(int, input().split())]
S = [*map(int, input().split())]
W = [[] for _ in range(100-P)]
M = 10**11
for i in range(100-P):
for j in range(N):
if i+1 <= A[j] <= i+1+P: W[i].append(S[j])
for w in W:
w.sort()
for i in range(len(w)-11):
if w[i+11]-w[i] <= Q: M = min(M, sum(w[i:i+12]))
if M == 10**11: print('NO')
else: print('YES', M)
83 changes: 83 additions & 0 deletions src/Finding Laputa/findinglaputa.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <bits/stdc++.h>
#include <bitset>
#define INF INT_MAX
using namespace std;

const int BUF = 1<<15;

char inbuf[BUF]; int inpos, inlen;
char next() {
if (inpos==inlen) {
inpos=0; inlen=(int)fread(inbuf,1,BUF,stdin);
if (!inlen) return EOF;
}
return inbuf[inpos++];
}
int read() {
char c;
while (!isdigit(c=next())) {}
int x=c-48;
while (isdigit(c=next())) x = x*10+(c-48);
return x;
}
int read_one_digit() {
char ch;
while (!isdigit(ch=next())) {}
return ch-48;
}

char outbuf[BUF], nbuf[20]; int outpos;
void flush_out() {
fwrite(outbuf, 1, outpos, stdout); outpos = 0;
}
void wchar(char c) {
if (outpos==BUF) flush_out();
outbuf[outpos++] = c;
}
void write(int x) {
int len = 0;
for (; x>9; x/=10) nbuf[len++] = (char)(48+(x%10));
wchar((char)(48+x));
while (len) wchar(nbuf[--len]);
wchar('\n');
}

int main() {
cin.tie(NULL);
cout.tie(NULL);
assert(atexit(flush_out)==0);
int N = read(), n = 1<<N;
vector<vector<pair<int, int>>> G(n);
vector<vector<int>> B(N+1);
vector<int> R;
for (int i = 0; i < n; i++) {
R.push_back(bitset<32>(i).count());
B[R[i]].push_back(i);
}
for (int i = 0; i < n; i++)
for (int j = 2; j < min(N, 5); j++)
for (int k : B[j]) G[i].push_back({i^k, 100*j*j+1});
for (int _ = 0; _ < N<<(N-1); _++) {
int a = read(), b = read(), w = read_one_digit();
G[a].push_back({b, 100*w}); G[b].push_back({a, 100*w});
}
vector<int> D(n, INF);
D[0] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, 0});
while (!pq.empty()) {
auto [dd, vv] = pq.top();
pq.pop();
if (dd != D[vv]) continue;
for (auto& [nn, weight] : G[vv]) {
int newDist = dd+weight;
if (D[nn] > newDist) {
D[nn] = newDist;
pq.push({newDist, nn});
}
}
}
write(D.back()/100);
write(D.back()%100);
return 0;
}
18 changes: 18 additions & 0 deletions src/Guaranteed Victory/guaranteedvictory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sys; input = sys.stdin.readline
N, M = map(int, input().split()); R = {}; E = []
for _ in range(N): s = input().strip(); R[s] = len(R)
for _ in range(M):
k = int(input()); p = input().strip().split()
for q in p:
if q not in R: R[q] = len(R)
for i in range(k-1): E.append((R[p[i+1]], R[p[0]]))
G = [[] for _ in range(len(R))]; I = [0]*len(R); T = []
for a, b in E: G[a].append(b); I[b] += 1
Q = [i for i in range(len(R)) if I[i] == 0]
for u in Q:
T.append(u)
for v in G[u]:
I[v] -= 1
if I[v] == 0: Q.append(v)
if len(T) == len(R): print('GUARANTEED VICTORY')
else: print(len(R)-len(T))
88 changes: 88 additions & 0 deletions src/Hatter's Hat Shop/hattershatshop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,bmi,bmi2,lzcnt")
using namespace std;

const int BUF = 1<<15;

char inbuf[BUF]; int inpos, inlen;
char next() {
if (inpos==inlen) {
inpos=0; inlen=(int)fread(inbuf,1,BUF,stdin);
//if (!inlen) return EOF;
}
return inbuf[inpos++];
}
int read() {
char c;
while (!isdigit(c=next())) {}
int x=c-48;
while (isdigit(c=next())) x = x*10+(c-48);
return x;
}

char outbuf[BUF], nbuf[20]; int outpos;
void flush_out() {
fwrite(outbuf, 1, outpos, stdout); outpos = 0;
}
void wchar(char c) {
if (outpos==BUF) flush_out();
outbuf[outpos++] = c;
}
void write(long long x) {
int len = 0;
for (; x>9; x/=10) nbuf[len++] = (char)(48+(x%10));
wchar((char)(48+x));
while (len) wchar(nbuf[--len]);
wchar('\n');
}

int main() {
cin.tie(NULL);
cout.tie(NULL);
assert(atexit(flush_out)==0);
int N = read(), M = read(), K = read();
vector<int> D(M), C(M); // increment and capacity
vector<vector<int>> P(M); // available hats
vector<vector<long long>> S(M, {0}); // prefix sum
long long Z = 0;
priority_queue<pair<long long, int>> Q;
vector<int> A(M, 0); // offset
for (int i = 0; i < M; ++i) {
D[i] = read(); C[i] = read();
}
for (int i = 0; i < N; ++i) {
int t = read(), s = read();
Z += s;
P[--t].push_back(C[t]-s);
}
for (int i = 0; i < M; ++i) {
sort(P[i].rbegin(), P[i].rend());
for (int j : P[i]) S[i].push_back(S[i].back()+j);
}
for (int i = 0; i < M; ++i) {
long long dz = 0;
for (int x : P[i]) dz += min(x, D[i]);
Q.emplace(dz, i);
}
while (K > 0 && !Q.empty()) {
// try to devise hat idx
auto [dz, idx] = Q.top();
Q.pop();
if (dz == 0) break;
// how many times can we upgrade
int u = max(min((P[idx].back()-A[idx])/D[idx], K), 1);
while (!P[idx].empty() && P[idx].back()-A[idx] <= D[idx]*u) P[idx].pop_back();
A[idx] += D[idx]*u; K -= u; Z += dz*u;
auto p = lower_bound(P[idx].begin(), P[idx].end(), D[idx]+A[idx], greater<int>())-P[idx].begin();
// -sum(min(P[idx][i]-A[idx], D[idx]) for i in range(len(P[idx])))
// -sum(min(P[idx][i], A[idx]+D[idx])-A[idx] for i in range(len(P[idx])))
// -sum(min(P[idx][i], A[idx]+D[idx]) for i in range(len(P[idx]))) + A[idx]*len(P[idx])
// -((A[idx]+D[idx])*p + {P[idx][p] + P[idx][p+1] + ... + P[idx][len(P[idx])-1])} + A[idx]*len(P[idx])
// -((A[idx]+D[idx])*p + {S[idx][len(P[idx])]-S[idx][p]}) + A[idx]*len(P[idx])
Q.emplace((A[idx]+D[idx])*p + S[idx][P[idx].size()]-S[idx][p] - A[idx]*P[idx].size(), idx);
}
write(Z);
return 0;
}
32 changes: 32 additions & 0 deletions src/Hatter's Hat Shop/hattershatshop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sys; input = sys.stdin.readline; from heapq import *; from bisect import *
N, M, K = map(int, input().split()); D = []; C = []; X = 10**9+7
for _ in range(M): d, c = map(int, input().split()); D.append(d); C.append(c)
P = [[] for _ in range(M)]; Z = 0; Q = []; A = [0]*M; S = [[0] for _ in range(M)]
for _ in range(N): t, s = map(int, input().split()); Z += s; P[t-1].append(C[t-1]-s)
for i in range(M):
P[i].sort(reverse=True)
for j in P[i]: S[i].append(S[i][-1]+j)
for i in range(M):
dz = 0
for x in P[i]: dz += min(x, D[i])
Q.append(-dz*X+i)
heapify(Q)
while K and Q:
# try to devise hat idx
ndz, idx = divmod(heappop(Q), X)
if not ndz: break
# how many times can we upgrade
u = max(min((P[idx][-1]-A[idx])//D[idx], K), 1)
while P[idx] and P[idx][-1]-A[idx] <= D[idx]*u: P[idx].pop()
A[idx] += D[idx]*u; K -= u; Z -= ndz*u; p, hi = 0, len(P[idx]); l = hi; z = D[idx]+A[idx]
while p < hi:
mi = (p+hi)//2
if P[idx][mi] < z: hi = mi
else: p = mi+1
# -sum(min(P[idx][i]-A[idx], D[idx]) for i in range(len(P[idx])))
# -sum(min(P[idx][i], A[idx]+D[idx])-A[idx] for i in range(len(P[idx])))
# -sum(min(P[idx][i], A[idx]+D[idx]) for i in range(len(P[idx]))) + A[idx]*len(P[idx])
# -((A[idx]+D[idx])*p + {P[idx][p] + P[idx][p+1] + ... + P[idx][len(P[idx])-1])} + A[idx]*len(P[idx])
# -((A[idx]+D[idx])*p + {S[idx][len(P[idx])]-S[idx][p]}) + A[idx]*len(P[idx])
heappush(Q, (-p*z-S[idx][l]+S[idx][p]+A[idx]*l)*X+idx)
print(Z)
61 changes: 61 additions & 0 deletions src/Hidden Camera/hiddencamera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
def intersect(s1, s2):
(p1, p2), (p3, p4) = s1, s2
(x1, y1), (x2, y2), (x3, y3), (x4, y4) = p1, p2, p3, p4
a, b, c = y2-y1, x1-x2, (y2-y1)*x1 - (x2-x1)*y1
d, e, f = y4-y3, x3-x4, (y4-y3)*x3 - (x4-x3)*y3
if a == b == 0: return ((x1, y1) if (x1, y1) == (x3, y3) else None) if d == e == 0 else ((x1, y1) if d*x1 + e*y1 == f and min(x3, x4) <= x1 <= max(x3, x4) and min(y3, y4) <= y1 <= max(y3, y4) else None)
elif d == e == 0: return (x3, y3) if a*x3 + b*y3 == c and min(x1, x2) <= x3 <= max(x1, x2) and min(y1, y2) <= y3 <= max(y1, y2) else None
else:
det = b*d-a*e
if det:
x, y = (b*f-c*e)/det, (c*d-a*f)/det
return (x, y) if min(x1, x2) <= x <= max(x1, x2) and min(y1, y2) <= y <= max(y1, y2) and min(x3, x4) <= x <= max(x3, x4) and min(y3, y4) <= y <= max(y3, y4) else None
else:
if a*f != c*d or b*f != c*e: return None
else:
p, q = min((x1, y1), (x2, y2)), max((x1, y1), (x2, y2))
r, s = min((x3, y3), (x4, y4)), max((x3, y3), (x4, y4))
if (p, q) == (r, s): i1, i2 = p, q
elif p <= r <= q: i1, i2 = r, min(q, s)
elif r <= p <= s: i1, i2 = p, min(s, q)
else: i1 = i2 = None
if i1 == i2 and i1 != None: return i1
elif i1: return None # (i1, i2)
else: return None

def area(p):
a, n = 0, len(p)
for i in range(n): a += p[i][0]*p[(i+1)%n][1] - p[i][1]*p[(i+1)%n][0]
return abs(a)/2

from math import *
for _ in range(int(input())):
N = int(input())
P = [[*map(int, input().split())] for _ in range(N)]
x1, y1 = P[0]; x2, y2 = P[1]
M = ((x1+x2)/2, (y1+y2)/2)
A = y2-y1; B = x1-x2; C = y1*x1-y2*x1-x1*y1+x2*y1
AL = A*cos(pi/4)+B*sin(pi/4)
BL = B*cos(pi/4)-A*sin(pi/4)
CL = (A-AL)*M[0]+(B-BL)*M[1]+C
AR = A*cos(pi/4)-B*sin(pi/4)
BR = B*cos(pi/4)+A*sin(pi/4)
CR = (A-AR)*M[0]+(B-BR)*M[1]+C
assert abs(A*M[0]+B*M[1]+C) < 1e-6
assert abs(AL*M[0]+BL*M[1]+CL) < 1e-6
assert abs(AR*M[0]+BR*M[1]+CR) < 1e-6
for d in range(6, 0, -1):
D = 10**d
L = ((M[0]-BL*D, M[1]+AL*D), (M[0]+BL*D, M[1]-AL*D))
R = ((M[0]-BR*D, M[1]+AR*D), (M[0]+BR*D, M[1]-AR*D))
l = r = None
for i in range(N-1):
S = (P[i+1], P[(i+2)%N])
K = intersect(S, L)
if K: l = (K, i)
K = intersect(S, R)
if K: r = (K, i)
if l and r: break
P2 = [l[0], M, r[0]]
for i in range(min(l[1], r[1]), max(l[1], r[1])): P2.append(P[i+2])
print(area(P2)/area(P))
Loading

0 comments on commit fff2497

Please sign in to comment.