-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
543bfa7
commit 43c0eaf
Showing
19 changed files
with
1,773 additions
and
698 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,30 @@ | ||
import sys; input = sys.stdin.readline; from array import * | ||
|
||
def ccw(p, q, r): | ||
return (q[0]-p[0])*(r[1]-p[1]) > (r[0]-p[0])*(q[1]-p[1]) | ||
|
||
def chull(pts): | ||
if len(pts) < 3: return pts | ||
pts, n = sorted(pts), len(pts) | ||
upper, lower = pts[:2], pts[-1:-3:-1] | ||
for i in range(2, n): | ||
while len(upper) > 1 and not ccw(upper[-2], upper[-1], pts[i]): upper.pop() | ||
upper.append(pts[i]) | ||
for i in range(n-2, -1, -1): | ||
while len(lower) > 1 and not ccw(lower[-2], lower[-1], pts[i]): lower.pop() | ||
lower.append(pts[i]) | ||
upper.pop(); lower.pop(); upper.extend(lower); return upper | ||
|
||
def area(x, y): | ||
return abs(c+x*dy-y*dx) | ||
|
||
for _ in range(int(input())): | ||
n = len(p:=chull([[*map(int, input().split())] for _ in range(int(input()))])); m = 0; X = array('i', (q[0] for q in p)); Y = array('i', (q[1] for q in p)) | ||
for i in range(n): | ||
a = i; b = i+1 # rotating caliper style | ||
for j in range(i+1, n): | ||
dx = X[i]-X[j]; dy = Y[i]-Y[j]; c = X[i]*Y[j]-X[j]*Y[i] | ||
while a < j and area(X[a], Y[a]) < area(X[a+1], Y[a+1]): a += 1 | ||
while b < n-1 and area(X[b], Y[b]) < area(X[b+1], Y[b+1]): b += 1 | ||
if (M:=area(X[a], Y[a])+area(X[b], Y[b])) > m: m = M | ||
print(str(m//2)+'.5'*(m%2)) |
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,16 @@ | ||
import sys; input = sys.stdin.readline; sys.setrecursionlimit(20000) | ||
|
||
def read(): | ||
global t, p | ||
n = ''; t += 1; x = t | ||
while p < len(s) and s[p] > ',': n += s[p]; p += 1 | ||
if p < len(s) and s[p] == '(': | ||
p += 1; l, l2, a = read(); p += 1; r, r2, b = read(); p += 1; z = f'{n}({l},{r})'; w = f'{n}({h[l2] if l2 in h else l},{h[r2] if r2 in h else r})' | ||
if w not in h: h[w] = x; return z, w, a+b+1 | ||
else: t -= a+b+1; return h[w], h[w], 0 | ||
else: | ||
z = n | ||
if z not in h: h[z] = x; return z, z, 1 | ||
t -= 1; return h[z], h[z], 0 | ||
|
||
for _ in range(int(input())): p = t = 0; s = input().strip(); h = {}; sys.stdout.write(read()[0]+'\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,31 @@ | ||
import sys; input = sys.stdin.readline | ||
for _ in range(int(input())): | ||
M = [input().strip() for _ in range(int(input()))] | ||
S = input().strip(); D = input().strip(); N = len(S); T = len(D); z = ['?']*T | ||
M = [i for i in M if len(i) == N]; can = 0 | ||
W = {chr(i+97):set() for i in range(26)} | ||
for m in M: | ||
a = {}; r = {}; ok = 1 | ||
for i in range(N): | ||
if m[i] not in a: a[m[i]] = S[i] | ||
if S[i] not in r: r[S[i]] = m[i] | ||
if a[m[i]] != S[i] or r[S[i]] != m[i]: ok = 0; break | ||
if ok: | ||
can = 1 | ||
for i in S: W[i].add(r[i]) | ||
if not can: print('IMPOSSIBLE'); continue | ||
if all(len(v)<2 for v in W.values()) and sum(len(v) for v in W.values()) == 25: | ||
for i in W: | ||
if not W[i]: | ||
t = {chr(i) for i in range(97, 123)} | ||
for j in W: t -= W[j] | ||
W[i] = t; break | ||
K = {chr(i+97):set() for i in range(26)} | ||
for i in W: | ||
for j in W[i]: K[j].add(i) | ||
for i in range(T): | ||
u = K[D[i]] | ||
if len(u) == 1: | ||
u = [*u][0] | ||
if len(W[u]) == 1: z[i] = u | ||
print(''.join(z)) |
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,117 @@ | ||
import sys; input = sys.stdin.readline; from array import *; INF = 10**9 | ||
|
||
class Vertex: | ||
def __init__(self, v): | ||
self.key, self.left, self.right, self.parent = v, None, None, None | ||
self.height, self.size, self.count = 0, 1, 1 | ||
|
||
class AVL: | ||
def __init__(self): | ||
self.root = None | ||
def search(self, v): | ||
res = self.helper_search(self.root, v) | ||
return res.key if res != None else INF | ||
def helper_search(self, t, v): | ||
if t == None: return None | ||
elif t.key == v: return t | ||
elif t.key < v: return self.helper_search(t.right, v) | ||
return self.helper_search(t.left, v) | ||
def find_min(self): | ||
return self.helper_find_min(self.root) | ||
def helper_find_min(self, t): | ||
if t == None: return INF | ||
if t.left == None: return t.key | ||
return self.helper_find_min(t.left) | ||
def successor(self, v): | ||
return self.helper_successor_plus(self.helper_search(self.root, v)) | ||
def helper_successor_plus(self, t): | ||
if t.right != None: return self.helper_find_min(t.right) | ||
else: | ||
par, cur = t.parent, t | ||
while par != None and cur == par.right: cur, par = par, par.parent | ||
if par == None: return INF | ||
else: return par.key | ||
def find_max(self): | ||
return self.helper_find_max(self.root) | ||
def helper_find_max(self, t): | ||
if t == None: return -1 | ||
if t.right == None: return t.key | ||
return self.helper_find_max(t.right) | ||
def predecessor(self, v): | ||
return self.helper_predecessor_plus(self.helper_search(self.root, v)) | ||
def helper_predecessor_plus(self, t): | ||
if t.left != None: return self.helper_find_max(t.left) | ||
else: | ||
par, cur = t.parent, t | ||
while par != None and cur == par.left: cur, par = par, par.parent | ||
if par == None: return -1 | ||
else: return par.key | ||
def update_height(self, t): | ||
if t.left != None and t.right != None: t.height = max(t.left.height, t.right.height) + 1 | ||
elif t.left != None: t.height = t.left.height + 1 | ||
elif t.right != None: t.height = t.right.height + 1 | ||
else: t.height = 1 | ||
def update_size(self, t): | ||
if t.left != None and t.right != None: t.size = t.left.size + t.right.size + t.count | ||
elif t.left != None: t.size = t.left.size + t.count | ||
elif t.right != None: t.size = t.right.size + t.count | ||
else: t.size = t.count | ||
def balance_factor(self, t): | ||
if t.left != None and t.right != None: return t.left.height - t.right.height | ||
if t.left != None: return t.left.height + 1 | ||
if t.right != None: return -1 - t.right.height | ||
return 0 | ||
def insert(self, v): | ||
def helper(t, v): | ||
if t == None: return Vertex(v) | ||
if t.key < v: t.right = helper(t.right, v); t.right.parent = t | ||
elif t.key > v: t.left = helper(t.left, v); t.left.parent = t | ||
else: t.count += 1 | ||
self.update_height(t), self.update_size(t); t = self.rebalance(t); return t | ||
self.root = helper(self.root, v) | ||
def delete(self, v): | ||
def helper(t, v): | ||
if t == None: return t | ||
if t.key < v: t.right = helper(t.right, v) | ||
elif t.key > v: t.left = helper(t.left, v) | ||
else: | ||
if t.count == 1: | ||
if t.left == None and t.right == None: t = None | ||
elif t.left == None and t.right != None: t.right.parent = t.parent; t = t.right | ||
elif t.left != None and t.right == None: t.left.parent = t.parent; t = t.left | ||
else: successor_v = self.successor(v); t.key = successor_v; t.right = helper(t.right, successor_v) | ||
else: t.count -= 1 | ||
if t != None: self.update_height(t), self.update_size(t); t = self.rebalance(t) | ||
return t | ||
self.root = helper(self.root, v) | ||
def rebalance(self, t): | ||
if t == None: return t | ||
if self.balance_factor(t) == 2: | ||
if self.balance_factor(t.left) == -1: t.left = self.left_rotate(t.left) | ||
t = self.right_rotate(t) | ||
elif self.balance_factor(t) == -2: | ||
if self.balance_factor(t.right) == 1: t.right = self.right_rotate(t.right) | ||
t = self.left_rotate(t) | ||
return t | ||
def left_rotate(self, t): | ||
w = t.right; w.parent = t.parent; t.parent = w; t.right = w.left | ||
if w.left != None: w.left.parent = t | ||
w.left = t; self.update_height(t), self.update_size(t), self.update_height(w), self.update_size(w); return w | ||
def right_rotate(self, t): | ||
w = t.left; w.parent = t.parent; t.parent = w; t.left = w.right | ||
if w.right != None: w.right.parent = t | ||
w.right = t; self.update_height(t), self.update_size(t), self.update_height(w), self.update_size(w); return w | ||
|
||
for t in range(int(input())): | ||
n = int(input()); s = []; h = array('i', [-1]*n) | ||
for _ in range(n): a, b, c = map(int, input().split()); s.append((a-1, c)); h[a-1] = b | ||
s.sort(key=lambda x: x[1]); p = AVL(); z = 0 | ||
for a, _ in s: | ||
p.insert(a); k = p.predecessor(a) | ||
if k != -1 and h[k] < h[a]: p.delete(a); continue | ||
z += 1 | ||
while True: | ||
v = p.successor(a) | ||
if v == INF or h[v] < h[a]: break | ||
p.delete(v) | ||
print(z) |
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,12 @@ | ||
import sys; input = sys.stdin.readline | ||
for _ in range(int(input())): | ||
T, N = map(int, input().split()) | ||
c = [1]*(T+1) | ||
for _ in range(N): | ||
d = [0]*(T+1); t1, t2 = map(int, input().split()); v = t2-t1 | ||
for i in range(T+1): | ||
if c[i] and i>=v: d[i-v] = 1 | ||
if c[i] and i<=T-v: d[i+v] = 1 | ||
c = d | ||
if sum(d) == 0: print('impossible'), exit(0) | ||
print('possible') |
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,23 @@ | ||
import sys; input = sys.stdin.readline | ||
while True: | ||
try: n, r = map(int, input().split()) | ||
except: break | ||
g = [{} for _ in range(n)]; t = [[] for _ in range(n)]; T = []; s = [(r-1, -1)]; d = [0]*n; z = [float('inf')]*n | ||
for _ in range(n-1): a, b, w = map(int, input().split()); g[a-1][b-1] = g[b-1][a-1] = w | ||
while s: | ||
u, p = s.pop() | ||
if p != -1: t[p].append(u); d[u] += 1 | ||
for x in g[u]: | ||
if x != p: s.append((x, u)) | ||
q = [i for i in range(n) if d[i] == 0] | ||
for u in q: | ||
T.append(u) | ||
for v in g[u]: | ||
d[v] -= 1 | ||
if d[v] == 0: q.append(v) | ||
while T: | ||
u = T.pop(); s = 0 | ||
if not t[u]: continue | ||
for v in t[u]: s += min(z[v], g[u][v]) | ||
z[u] = s | ||
print(z[r-1]) |
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,9 @@ | ||
import sys; input = sys.stdin.readline; from array import *; M = 10201; g = [0]*M*M; p = sorted((i*i+j*j,i,j) for i in range(-100, 101) for j in range(-100, 101) if i*i+j*j<=10**4); X = array('B', [k[1]+100 for k in p]); Y = array('B', [k[2]+100 for k in p]); l = array('h'); c = 0 | ||
for i in range(len(p)): | ||
if (X[i]-100)**2+(Y[i]-100)**2>c*c: l.append(i); c += 1 | ||
l.append(len(p)) | ||
for _ in range(int(input())): x, y = map(int, input().split()); g[M*(x+100)+(y+100)] += 1 | ||
for _ in range(int(input())): | ||
x, y, r = map(int, input().split()) | ||
for i in range(l[r]): g[M*(x+X[i])+y+Y[i]] = 0 | ||
print(sum(g)) |
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,16 @@ | ||
import sys; input = sys.stdin.readline | ||
K = ((0, 1), (0, -1), (-1, 0), (1, 0)); R, C = map(int, input().split()); M = [input().strip() for _ in range(R)]; W = set() | ||
|
||
def bt(r, c, p, h): | ||
p.append(M[r][c]); h.add(r*C+c) | ||
if len(p) <= 10: W.add(''.join(p)) | ||
if len(p) != 10: | ||
for dr, dc in K: | ||
if R>r+dr>-1<c+dc<C and (r+dr)*C+c+dc not in h: bt(r+dr, c+dc, p, h) | ||
h.discard(r*C+c); p.pop() | ||
|
||
p, h, w = [], set(), 0 | ||
for r in range(R): | ||
for c in range(C): bt(r, c, p, h) | ||
for i in range(int(input())): w += input().strip() in W | ||
print(w) |
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 @@ | ||
import sys; input = sys.stdin.readline | ||
while (n:=int(input())): | ||
k = input().strip().upper().replace(' ', '').replace('J', 'I'); K = [] | ||
for i in k: | ||
if i not in K: K.append(i) | ||
for i in range(65, 91): | ||
if i != 74 and chr(i) not in K: K.append(chr(i)) | ||
m = {e:i for i,e in enumerate(K)} | ||
for _ in range(n): | ||
s = [*input().strip().upper().replace(' ', '').replace('J', 'I')[::-1]]; t = [] | ||
while s: | ||
if len(s) == 1: t.append(s.pop()+'X'); break | ||
a, b = s.pop(), s.pop() | ||
if a == b: s.append(b); t.append(a+'X') | ||
else: t.append(a+b) | ||
z = [] | ||
for a, b in t: | ||
pa, pb = m[a], m[b] | ||
if a == b == 'X': z.append('YY') | ||
elif pa//5 != pb//5 and pa%5 != pb%5: z.append(K[5*(pa//5)+pb%5]+K[5*(pb//5)+pa%5]) | ||
elif pa%5 == pb%5: z.append(K[5*((pa//5+1)%5)+pa%5]+K[5*((pb//5+1)%5)+pb%5]) | ||
else: z.append(K[5*(pa//5)+(pa%5+1)%5]+K[5*(pb//5)+(pb%5+1)%5]) | ||
print(''.join(z)) | ||
print() |
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,28 @@ | ||
import sys; input = sys.stdin.readline | ||
P = [1<<i for i in range(30)] | ||
for _ in range(int(input())): | ||
n, m = map(int, input().split()); B = []; C = []; H = {}; S = None; U = 1 | ||
for _ in range(n): b, c = input().split(); B.append(int(b, 2)); C.append(int(c)) | ||
for bm in range(1<<(m//2)): | ||
z = [] | ||
for i in range(n): | ||
c = 0 | ||
for j in range(m//2): c += bool(bm&P[j])==bool(B[i]&P[j]) | ||
z.append(c) | ||
z = tuple(z) | ||
if z not in H: H[z] = [] | ||
H[z].append(bm) | ||
for bm in range(1<<(m-m//2)): | ||
z = [] | ||
for i in range(n): | ||
c = 0 | ||
for j in range(m-m//2): c += bool(bm&P[j])==bool(B[i]&P[j+m//2]) | ||
z.append(c) | ||
d = tuple(C[i]-z[i] for i in range(n)) | ||
if d not in H: continue | ||
if S == None and len(H[d]) == 1: S = (bm<<(m//2))+H[d][0] | ||
elif U: S = (S!=None)+len(H[d]); U = 0 | ||
else: S += len(H[d]) | ||
if S == None: print('0 solutions') | ||
elif U: print(bin(S)[2:].zfill(m)) | ||
else: print(S, 'solutions') |
19 changes: 19 additions & 0 deletions
19
src/Longest Common Subsequence/longestcommonsubsequence.py
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,19 @@ | ||
import sys; input = sys.stdin.readline | ||
n, k = map(int, input().split()) | ||
c = {(i, j) for i in range(k) for j in range(k)} | ||
for _ in range(n): | ||
s = input().strip() | ||
for i in range(k): | ||
for j in range(i+1): c.discard((ord(s[i])-65, ord(s[j])-65)) | ||
G = [[] for _ in range(k)]; D = [0]*k | ||
for a, b in c: G[a].append(b); D[b] += 1 | ||
T = []; Q = [i for i in range(k) if D[i] == 0]; Z = [1]*k | ||
for u in Q: | ||
T.append(u) | ||
for v in G[u]: | ||
D[v] -= 1 | ||
if D[v] == 0: Q.append(v) | ||
while T: | ||
u = T.pop() | ||
for v in G[u]: Z[u] = max(Z[u], Z[v]+1) | ||
print(max(Z)) |
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,14 @@ | ||
import sys; input = sys.stdin.readline; L = 2 | ||
while (n:=int(input())): | ||
h = {}; m = 0 | ||
for _ in range(n): c, g, s, b = input().split(); h[c] = (int(g), int(s), int(b)); m += sum(h[c]) | ||
if 'Canada' not in h: print('Canada cannot win.'); continue | ||
p = [m**i for i in range(L)]; ok = 0 | ||
for j in range(L): | ||
if ok: break | ||
for k in range(L): | ||
if ok: break | ||
for l in range(L): | ||
z = {c:g*p[j]+s*p[k]+b*p[l] for c, (g, s, b) in h.items()} | ||
if max(z.values()) == z['Canada']: ok = 1; break | ||
print('Canada', ['cannot win.', 'wins!'][ok]) |
Oops, something went wrong.