From 6068c6c33aff045b878cefb44aff2e9957d22582 Mon Sep 17 00:00:00 2001 From: delena0702 <57484961+delena0702@users.noreply.github.com> Date: Sat, 30 Jul 2022 17:19:53 +0900 Subject: [PATCH] 22.07.30 --- BAEKJOON/11438_LCA_2.py | 63 +++++++++++++++++++ "BAEKJOON/1184_\352\267\200\353\206\215.py" | 41 ++++++++++++ BAEKJOON/13528_Grass_Seed_Inc..py | 5 ++ .../14928_\355\201\260_\354\210\230_(BIG).py" | 4 ++ ...54\232\264_\354\226\270\354\226\264_CC.py" | 5 ++ ...1\355\230\225_\353\263\265\354\233\220.py" | 17 +++++ ...4\355\230\270_\355\225\264\353\217\205.py" | 14 +++++ BAEKJOON/9248_Suffix_Array.py | 47 ++++++++++++++ 8 files changed, 196 insertions(+) create mode 100644 BAEKJOON/11438_LCA_2.py create mode 100644 "BAEKJOON/1184_\352\267\200\353\206\215.py" create mode 100644 BAEKJOON/13528_Grass_Seed_Inc..py create mode 100644 "BAEKJOON/14928_\355\201\260_\354\210\230_(BIG).py" create mode 100644 "BAEKJOON/19945_\354\203\210\353\241\234\354\232\264_\354\226\270\354\226\264_CC.py" create mode 100644 "BAEKJOON/2103_\354\247\201\352\265\220\353\213\244\352\260\201\355\230\225_\353\263\265\354\233\220.py" create mode 100644 "BAEKJOON/2149_\354\225\224\355\230\270_\355\225\264\353\217\205.py" create mode 100644 BAEKJOON/9248_Suffix_Array.py diff --git a/BAEKJOON/11438_LCA_2.py b/BAEKJOON/11438_LCA_2.py new file mode 100644 index 0000000..57b564d --- /dev/null +++ b/BAEKJOON/11438_LCA_2.py @@ -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) diff --git "a/BAEKJOON/1184_\352\267\200\353\206\215.py" "b/BAEKJOON/1184_\352\267\200\353\206\215.py" new file mode 100644 index 0000000..bca307c --- /dev/null +++ "b/BAEKJOON/1184_\352\267\200\353\206\215.py" @@ -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) \ No newline at end of file diff --git a/BAEKJOON/13528_Grass_Seed_Inc..py b/BAEKJOON/13528_Grass_Seed_Inc..py new file mode 100644 index 0000000..4461356 --- /dev/null +++ b/BAEKJOON/13528_Grass_Seed_Inc..py @@ -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)]))) \ No newline at end of file diff --git "a/BAEKJOON/14928_\355\201\260_\354\210\230_(BIG).py" "b/BAEKJOON/14928_\355\201\260_\354\210\230_(BIG).py" new file mode 100644 index 0000000..4821b03 --- /dev/null +++ "b/BAEKJOON/14928_\355\201\260_\354\210\230_(BIG).py" @@ -0,0 +1,4 @@ +import sys +def input(): return sys.stdin.readline().strip() + +print(int(input()) % 20000303) \ No newline at end of file diff --git "a/BAEKJOON/19945_\354\203\210\353\241\234\354\232\264_\354\226\270\354\226\264_CC.py" "b/BAEKJOON/19945_\354\203\210\353\241\234\354\232\264_\354\226\270\354\226\264_CC.py" new file mode 100644 index 0000000..7343b11 --- /dev/null +++ "b/BAEKJOON/19945_\354\203\210\353\241\234\354\232\264_\354\226\270\354\226\264_CC.py" @@ -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) \ No newline at end of file diff --git "a/BAEKJOON/2103_\354\247\201\352\265\220\353\213\244\352\260\201\355\230\225_\353\263\265\354\233\220.py" "b/BAEKJOON/2103_\354\247\201\352\265\220\353\213\244\352\260\201\355\230\225_\353\263\265\354\233\220.py" new file mode 100644 index 0000000..fb66764 --- /dev/null +++ "b/BAEKJOON/2103_\354\247\201\352\265\220\353\213\244\352\260\201\355\230\225_\353\263\265\354\233\220.py" @@ -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) \ No newline at end of file diff --git "a/BAEKJOON/2149_\354\225\224\355\230\270_\355\225\264\353\217\205.py" "b/BAEKJOON/2149_\354\225\224\355\230\270_\355\225\264\353\217\205.py" new file mode 100644 index 0000000..7c08334 --- /dev/null +++ "b/BAEKJOON/2149_\354\225\224\355\230\270_\355\225\264\353\217\205.py" @@ -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)) \ No newline at end of file diff --git a/BAEKJOON/9248_Suffix_Array.py b/BAEKJOON/9248_Suffix_Array.py new file mode 100644 index 0000000..ef2f3ff --- /dev/null +++ b/BAEKJOON/9248_Suffix_Array.py @@ -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=' ')