Skip to content

Commit

Permalink
22.07.30
Browse files Browse the repository at this point in the history
  • Loading branch information
delena0702 authored Jul 30, 2022
1 parent 35d36cf commit 6068c6c
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 0 deletions.
63 changes: 63 additions & 0 deletions BAEKJOON/11438_LCA_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from collections import deque
from math import log2
import sys
def input(): return sys.stdin.readline().strip()


N = int(input())

edges = [[] for _ in range(N + 1)]
for _ in range(N - 1):
a, b = map(int, input().split())
edges[a].append(b)
edges[b].append(a)

ancestor = [[] for _ in range(N + 1)]
depth = [-1] * (N + 1)
depth[1] = 0
visited = [False] * (N + 1)
visited[1] = True

queue = deque([1])
while queue:
here = queue.popleft()

for there in edges[here]:
if visited[there]:
continue

visited[there] = True
depth[there] = depth[here] + 1
ancestor[there].append(here)
child, idx = here, 0
while True:
if len(ancestor[child]) <= idx:
break

ancestor[there].append(ancestor[child][idx])
child = ancestor[child][idx]
idx = idx + 1
queue.append(there)

M = int(input())
for _ in range(M):
a, b = map(int, input().split())
if depth[a] > depth[b]:
a, b = b, a

d = depth[b] - depth[a]
while d:
b = ancestor[b][int(log2(-d & d))]
d = d - (-d & d)

while a != b:
s, e = 0, len(ancestor[a])
while s < e:
m = (s + e) // 2
if ancestor[a][m] == ancestor[b][m]:
e = m
else:
s = m + 1

a, b = ancestor[a][max(0, s - 1)], ancestor[b][max(0, s - 1)]
print(a)
41 changes: 41 additions & 0 deletions BAEKJOON/1184_귀농.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from collections import defaultdict
import sys
def input(): return sys.stdin.readline().strip()

def getArea(x1, y1, x2, y2):
if x1 > x2:
x1, x2 = x2, x1
if y1 > y2:
y1, y2 = y2, y1
return subsum[y2][x2] + subsum[y1][x1] -\
subsum[y2][x1] - subsum[y1][x2]

N = int(input())
data = [list(map(int, input().split())) for _ in range(N)]
subsum = [[0] * (N + 1) for _ in range(N + 1)]
for i in range(N + 1):
for j in range(N + 1):
if i and j:
subsum[i][j] = subsum[i-1][j] + subsum[i][j-1] +\
data[i-1][j-1] - subsum[i-1][j-1]

answer = 0
for i in range((N + 1) ** 2):
y, x = divmod(i, N + 1)
counter = [defaultdict(int), defaultdict(int)]

for j in range((N + 1) ** 2):
ty, tx = divmod(j, N + 1)
s = getArea(x, y, tx, ty)

if ty < y:
if tx < x:
counter[0][s] = counter[0][s] + 1
elif tx > x:
counter[1][s] = counter[1][s] + 1
elif ty > y:
if tx < x:
answer = answer + counter[1][s]
elif tx > x:
answer = answer + counter[0][s]
print(answer)
5 changes: 5 additions & 0 deletions BAEKJOON/13528_Grass_Seed_Inc..py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys
def input(): return sys.stdin.readline().strip()

C, L = float(input()), int(input())
print(C * sum(map(lambda x: x[0] * x[1], [list(map(float, input().split())) for _ in range(L)])))
4 changes: 4 additions & 0 deletions BAEKJOON/14928_큰_수_(BIG).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import sys
def input(): return sys.stdin.readline().strip()

print(int(input()) % 20000303)
5 changes: 5 additions & 0 deletions BAEKJOON/19945_새로운_언어_CC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys
def input(): return sys.stdin.readline().strip()

N = int(input())
print((len(bin(N)) - 2) if N >= 0 else 32)
17 changes: 17 additions & 0 deletions BAEKJOON/2103_직교다각형_복원.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from collections import defaultdict
import sys
def input(): return sys.stdin.readline().strip()

N = int(input())
dic = defaultdict(list)
for _ in range(N):
x, y = map(int, input().split())
dic[x].append(y + 20000)
dic[y + 20000].append(x)

answer = 0
for arr in dic.values():
arr.sort()
for i in range(0, len(arr), 2):
answer = answer + arr[i + 1] - arr[i]
print(answer)
14 changes: 14 additions & 0 deletions BAEKJOON/2149_암호_해독.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import sys
def input(): return sys.stdin.readline().strip()

key, data = input(), input()
N, M = len(data), len(key)

order = [i for i in range(M)]
order.sort(key=lambda x: key[x])

answer = ['_'] * N
for i, ch in enumerate(data):
x, y = divmod(i, N // M)
answer[M * y + order[x]] = ch
print(''.join(answer))
47 changes: 47 additions & 0 deletions BAEKJOON/9248_Suffix_Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import sys
def input(): return sys.stdin.readline().strip()


def getKey(x):
return (rank[x], rank[min(N, x + d)])


data = input()
N = len(data)

data = data + "$"
sa = [i for i in range(N)]
rank = [ord(ch) for ch in data] + [0]
next_rank = [0] * (N + 1)

d = 1
while d < N:
sa.sort(key=getKey)
next_rank[sa[0]] = 1
for i in range(1, N):
next_rank[sa[i]] = next_rank[sa[i - 1]]
if getKey(sa[i - 1]) < getKey(sa[i]):
next_rank[sa[i]] = next_rank[sa[i]] + 1
rank = next_rank[:]
d = d << 1

isa = [0] * N
for i in range(N):
isa[sa[i]] = i

k = 0
lcp = [-1] * N
for i in range(0, N):
if isa[i] == 0:
continue

j = sa[isa[i] - 1]
while data[i + k] == data[j + k]:
k = k + 1

lcp[isa[i]] = k
k = max(0, k - 1)

print(*map(lambda x: x + 1, sa), sep=' ')
print("x", end=' ')
print(*map(lambda x: x, lcp[1:]), sep=' ')

0 comments on commit 6068c6c

Please sign in to comment.