From 61ab728e6f08387beb29549c87f0772f5296537f Mon Sep 17 00:00:00 2001 From: "Geonwoo, Shin" Date: Mon, 25 Mar 2024 01:05:06 +0900 Subject: [PATCH 1/8] =?UTF-8?q?add:=20BOJ=5F14002=5F=EA=B0=80=EC=9E=A5=20?= =?UTF-8?q?=EA=B8=B4=20=EC=A6=9D=EA=B0=80=ED=95=98=EB=8A=94=20=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20=EC=88=98=EC=97=B4=204.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...353\266\204 \354\210\230\354\227\264 4.py" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "larcenous/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.py" diff --git "a/larcenous/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.py" "b/larcenous/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.py" new file mode 100644 index 0000000..1ce8327 --- /dev/null +++ "b/larcenous/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.py" @@ -0,0 +1,23 @@ +N = int(input()) +A = list(map(int,input().split())) + +DP = [1 for _ in range(N)] + +for i in range(N): + for j in range(i): + if A[i] > A[j]: + DP[i] = max(DP[i], DP[j]+1) + +max_len = max(DP) +print(max_len) +max_idx = DP.index(max_len) +LIS = [] + +while max_idx >= 0 : #거꾸로 순회 + if DP[max_idx] == max_len : + LIS.append(A[max_idx]) + max_len -= 1 + max_idx -= 1 + +LIS.reverse() +print(*LIS) \ No newline at end of file From 23f1dfb2b825222229f2abc59a407894601f8ff1 Mon Sep 17 00:00:00 2001 From: "Geonwoo, Shin" Date: Mon, 25 Mar 2024 19:56:43 +0900 Subject: [PATCH 2/8] =?UTF-8?q?add:=20BOJ=5F14003=5F=EA=B0=80=EC=9E=A5=20?= =?UTF-8?q?=EA=B8=B4=20=EC=A6=9D=EA=B0=80=ED=95=98=EB=8A=94=20=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20=EC=88=98=EC=97=B4=205.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...353\266\204 \354\210\230\354\227\264 5.py" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "larcenous/week_15/BOJ_14003_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 5.py" diff --git "a/larcenous/week_15/BOJ_14003_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 5.py" "b/larcenous/week_15/BOJ_14003_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 5.py" new file mode 100644 index 0000000..570b26d --- /dev/null +++ "b/larcenous/week_15/BOJ_14003_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 5.py" @@ -0,0 +1,51 @@ +import sys +INF = sys.maxsize +N = int(input()) +A = list(map(int,input().split())) #부분 수열이므로 정렬 불가 + +#투 포인터로는 불가? +#upper bound binary search +''' +lower bound는 타겟보다 같거나 큰 값이 나오는 처음 위치를 찾습니다. + +upper bound는 타겟보다 처음으로 큰 값이 나오는 위치를 찾습니다. +''' +def bi_search(LIS,target): #중요 + left, right = -1,len(LIS) + while left+1 < right: + mid = (left + right) // 2 + if LIS[mid] < target: + left = mid + else: + right = mid + return right + +LIS = [-INF] +LIS_total = [(-INF,0)] #역추적을 위해 idx를 함께 저장 + +A = A[::-1] #맨 앞부터 pop을 통해 활용하기 위해 + +while A : + num = A.pop() + if num > LIS[-1] : + LIS_total.append((num,len(LIS))) + LIS.append(num) + + else : + idx = bi_search(LIS,num) + LIS[idx] = num + LIS_total.append((num,idx)) + +ans = [] +LIS_len = len(LIS)-1 + +while LIS_total : + if LIS_len <= 0 : + break + num, idx = LIS_total.pop() + if idx == LIS_len: + ans.append(num) + LIS_len -= 1 + +print(len(ans)) +print(*ans[::-1]) \ No newline at end of file From 1f34865de2daa66cb49bb3e5c1df9257a0d40943 Mon Sep 17 00:00:00 2001 From: "Geonwoo, Shin" Date: Tue, 26 Mar 2024 23:45:00 +0900 Subject: [PATCH 3/8] add: BOJ_9252_LCS 2.py --- larcenous/week_15/BOJ_9252_LCS 2.py | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 larcenous/week_15/BOJ_9252_LCS 2.py diff --git a/larcenous/week_15/BOJ_9252_LCS 2.py b/larcenous/week_15/BOJ_9252_LCS 2.py new file mode 100644 index 0000000..d8c6893 --- /dev/null +++ b/larcenous/week_15/BOJ_9252_LCS 2.py @@ -0,0 +1,35 @@ +A = input().rstrip() +B = input().rstrip() + +def LCS_DP(A,B) : + DP = [[0]*(len(B)+1) for _ in range(len(A)+1)] + for i in range(1,len(A)+1): + for j in range(1,len(B)+1): + if A[i-1] != B[j-1] : + DP[i][j]=max(DP[i][j-1],DP[i-1][j]) + else : + DP[i][j]=DP[i-1][j-1]+1 + return DP + +#print(LCS_DP(A,B)) + +def LCS(DP) : #역추적을 하는 함수 + result = [] + i, j = len(A),len(B) #DP 2차원 배열의 가로 세로 끝 index 지정 + maximum = DP[-1][-1] + print(maximum) + if maximum > 0 : + while maximum != 0 and i > 0 and j > 0: + if maximum == DP[i-1][j] : + i-=1 + elif maximum == DP[i][j-1] : + j-=1 + else : + result.append(A[i-1]) + maximum -= 1 + i-=1 + j-=1 + print(*result[::-1], sep="") + +tmp = LCS_DP(A,B) +LCS(tmp) \ No newline at end of file From 7e21fe7e50ce0049d76afcbf72105f78940ff2ac Mon Sep 17 00:00:00 2001 From: "Geonwoo, Shin" Date: Fri, 29 Mar 2024 03:32:25 +0900 Subject: [PATCH 4/8] =?UTF-8?q?add:=20BOJ=5F11779=5F=EC=B5=9C=EC=86=8C?= =?UTF-8?q?=EB=B9=84=EC=9A=A9=20=EA=B5=AC=ED=95=98=EA=B8=B0=202.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\265\254\355\225\230\352\270\260 2.py" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 "larcenous/week_15/BOJ_11779_\354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260 2.py" diff --git "a/larcenous/week_15/BOJ_11779_\354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260 2.py" "b/larcenous/week_15/BOJ_11779_\354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260 2.py" new file mode 100644 index 0000000..5050135 --- /dev/null +++ "b/larcenous/week_15/BOJ_11779_\354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260 2.py" @@ -0,0 +1,46 @@ +import sys +from collections import defaultdict +import heapq + +INF = sys.maxsize +input = sys.stdin.readline + +n = int(input()) +m = int(input()) +graph = defaultdict(list) +#graph 구성 +for _ in range(m): + a, b, c = map(int, input().split()) + graph[a].append((b, c)) +start, end = map(int, input().split()) + +dist = [INF] * (n+1) +prev_node = [0] * (n+1) #이전 노드값을 저장하여 역추적에 사용 + +def dijkstra(start): #start에서 각 노드로 향하는 최단거리 계산 + q = [] + heapq.heappush(q, (0, start)) + dist[start] = 0 + while q: + weight, node = heapq.heappop(q) + if dist[node] < weight: + continue + for adj_node, adj_weight in graph[node]: + cost = weight + adj_weight + if cost < dist[adj_node]: + dist[adj_node] = cost + prev_node[adj_node] = node + heapq.heappush(q, (cost, adj_node)) + +dijkstra(start) +print(dist[end]) + +#역추적 진행 +path = [end] +now = end +while now != start: + now = prev_node[now] + path.append(now) + +print(len(path)) +print(*path[::-1]) \ No newline at end of file From 59d0b690e63ac8544c5223a6d9448edd4a90dcf5 Mon Sep 17 00:00:00 2001 From: "Geonwoo, Shin" Date: Sat, 30 Mar 2024 00:19:26 +0900 Subject: [PATCH 5/8] add: BOJ_9019_DSLR.py --- larcenous/week_15/BOJ_9019_DSLR.py | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 larcenous/week_15/BOJ_9019_DSLR.py diff --git a/larcenous/week_15/BOJ_9019_DSLR.py b/larcenous/week_15/BOJ_9019_DSLR.py new file mode 100644 index 0000000..de86cea --- /dev/null +++ b/larcenous/week_15/BOJ_9019_DSLR.py @@ -0,0 +1,40 @@ +''' +회전한다는 말에 deque를 쓰면 시간초과가 나기 쉽다 +''' +import sys +from collections import deque +input = sys.stdin.readline +T = int(input()) +def BFS(A,B) : + visited = [False]*10001 + q = deque([(A,'')]) + visited[A] = True + while q : + for _ in range(len(q)) : + now = q.popleft() + now_d, now_ins = now[0], now[1] + if now_d == B : + return now_ins + tmp = (2*now_d)%10000 + if not visited[tmp] : + visited[tmp] = True + q.append((tmp,now_ins+'D')) + + tmp = (now_d-1)%10000 + if not visited[tmp] : + visited[tmp] = True + q.append((tmp,now_ins+'S')) + + tmp = now_d // 1000 + (now_d%1000)*10 + if not visited[tmp] : + visited[tmp] = True + q.append((tmp,now_ins+'L')) + + tmp = now_d // 10 + (now_d%10)*1000 + if not visited[tmp] : + visited[tmp] = True + q.append((tmp,now_ins+'R')) + +for _ in range(T) : + A,B = map(int,input().split()) + print(*BFS(A,B),sep='') \ No newline at end of file From 7ac04d0c61330afe4d43b25670b3c17e27af6251 Mon Sep 17 00:00:00 2001 From: "Geonwoo, Shin" Date: Sun, 31 Mar 2024 20:28:53 +0900 Subject: [PATCH 6/8] =?UTF-8?q?del:=20BOJ=5F14003=5F=EA=B0=80=EC=9E=A5=20?= =?UTF-8?q?=EA=B8=B4=20=EC=A6=9D=EA=B0=80=ED=95=98=EB=8A=94=20=EB=B6=80?= =?UTF-8?q?=EB=B6=84=20=EC=88=98=EC=97=B4=205.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...353\266\204 \354\210\230\354\227\264 5.py" | 51 ------------------- 1 file changed, 51 deletions(-) delete mode 100644 "larcenous/week_15/BOJ_14003_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 5.py" diff --git "a/larcenous/week_15/BOJ_14003_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 5.py" "b/larcenous/week_15/BOJ_14003_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 5.py" deleted file mode 100644 index 570b26d..0000000 --- "a/larcenous/week_15/BOJ_14003_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 5.py" +++ /dev/null @@ -1,51 +0,0 @@ -import sys -INF = sys.maxsize -N = int(input()) -A = list(map(int,input().split())) #부분 수열이므로 정렬 불가 - -#투 포인터로는 불가? -#upper bound binary search -''' -lower bound는 타겟보다 같거나 큰 값이 나오는 처음 위치를 찾습니다. - -upper bound는 타겟보다 처음으로 큰 값이 나오는 위치를 찾습니다. -''' -def bi_search(LIS,target): #중요 - left, right = -1,len(LIS) - while left+1 < right: - mid = (left + right) // 2 - if LIS[mid] < target: - left = mid - else: - right = mid - return right - -LIS = [-INF] -LIS_total = [(-INF,0)] #역추적을 위해 idx를 함께 저장 - -A = A[::-1] #맨 앞부터 pop을 통해 활용하기 위해 - -while A : - num = A.pop() - if num > LIS[-1] : - LIS_total.append((num,len(LIS))) - LIS.append(num) - - else : - idx = bi_search(LIS,num) - LIS[idx] = num - LIS_total.append((num,idx)) - -ans = [] -LIS_len = len(LIS)-1 - -while LIS_total : - if LIS_len <= 0 : - break - num, idx = LIS_total.pop() - if idx == LIS_len: - ans.append(num) - LIS_len -= 1 - -print(len(ans)) -print(*ans[::-1]) \ No newline at end of file From f8273f1b63d069888176d56fbabe218205905780 Mon Sep 17 00:00:00 2001 From: "Geonwoo, Shin" Date: Sun, 31 Mar 2024 20:30:24 +0900 Subject: [PATCH 7/8] =?UTF-8?q?add:=20BOJ=5F12852=5F1=EB=A1=9C=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A4=EA=B8=B0=202.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\247\214\353\223\244\352\270\260 2.py" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 "larcenous/week_15/BOJ_12852_1\353\241\234 \353\247\214\353\223\244\352\270\260 2.py" diff --git "a/larcenous/week_15/BOJ_12852_1\353\241\234 \353\247\214\353\223\244\352\270\260 2.py" "b/larcenous/week_15/BOJ_12852_1\353\241\234 \353\247\214\353\223\244\352\270\260 2.py" new file mode 100644 index 0000000..5c21551 --- /dev/null +++ "b/larcenous/week_15/BOJ_12852_1\353\241\234 \353\247\214\353\223\244\352\270\260 2.py" @@ -0,0 +1,21 @@ +from collections import * +N = int(input()) +def BFS(N) : + q = deque([[N]]) + cnt = 0 + while q : + for _ in range(len(q)) : + path = q.popleft() + x = path[-1] + if x == 1 : + return cnt, path + if x%3 == 0 : + q.append(path + [x//3]) + if x%2 == 0 : + q.append(path + [x//2]) + q.append(path + [x-1]) + cnt += 1 + +cnt, path = BFS(N) +print(cnt) +print(*path) \ No newline at end of file From c0421d8a949ad0120e54be9228b97d0538141dba Mon Sep 17 00:00:00 2001 From: "Geonwoo, Shin" Date: Sun, 31 Mar 2024 20:30:30 +0900 Subject: [PATCH 8/8] =?UTF-8?q?add:=20BOJ=5F13913=5F=EC=88=A8=EB=B0=94?= =?UTF-8?q?=EA=BC=AD=EC=A7=88=204.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\260\224\352\274\255\354\247\210 4.py" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "larcenous/week_15/BOJ_13913_\354\210\250\353\260\224\352\274\255\354\247\210 4.py" diff --git "a/larcenous/week_15/BOJ_13913_\354\210\250\353\260\224\352\274\255\354\247\210 4.py" "b/larcenous/week_15/BOJ_13913_\354\210\250\353\260\224\352\274\255\354\247\210 4.py" new file mode 100644 index 0000000..1de8d82 --- /dev/null +++ "b/larcenous/week_15/BOJ_13913_\354\210\250\353\260\224\352\274\255\354\247\210 4.py" @@ -0,0 +1,32 @@ +from collections import deque +N, K = map(int,input().split()) +time = [-1]*(100001) +def BFS(N,K) : + q = deque([N]) + t = 0 + time[N] = (0,N) + if N == K : + return time[N][1], time[N][0] + while q : + for _ in range(len(q)) : + current = q.popleft() + dx=[current+1,current-1,current*2] + for x in dx : + if 0<=x<=100000 and time[x]==-1: + time[x] = (t+1,current) + q.append(x) + if x == K : + return x, t+1 + t += 1 + +def Backtracking(end,t) : + res = [end] + while t != 0 : + end = time[end][-1] + res.append(end) + t -= 1 + return res[::-1] + +end, t = BFS(N,K) +print(t) +print(*Backtracking(end,t)) \ No newline at end of file