From bbbe3ad6875972fb17025c988e702be6144ca577 Mon Sep 17 00:00:00 2001 From: YoonYn9915 Date: Thu, 5 Jun 2025 17:24:08 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[BOJ]=20=EC=A0=84=EC=83=9D=ED=96=88?= =?UTF-8?q?=EB=8D=94=EB=8B=88=20=EC=8A=AC=EB=9D=BC=EC=9E=84=20=EC=97=B0?= =?UTF-8?q?=EA=B5=AC=EC=9E=90=EC=98=80=EB=8D=98=20=EA=B1=B4=EC=97=90=20?= =?UTF-8?q?=EB=8C=80=ED=95=98=EC=97=AC=20(Hard)=20/=20=EA=B3=A8=EB=93=9C?= =?UTF-8?q?=204=20/=2080=EB=B6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acmicpc.net/problem/14698 --- ...214\200\355\225\230\354\227\254 (Hard).py" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 "YoonYn9915/greedy/2025-06-03-[\353\260\261\354\244\200]-#14698-\354\240\204\354\203\235\355\226\210\353\215\224\353\213\210 \354\212\254\353\235\274\354\236\204 \354\227\260\352\265\254\354\236\220\354\230\200\353\215\230 \352\261\264\354\227\220 \353\214\200\355\225\230\354\227\254 (Hard).py" diff --git "a/YoonYn9915/greedy/2025-06-03-[\353\260\261\354\244\200]-#14698-\354\240\204\354\203\235\355\226\210\353\215\224\353\213\210 \354\212\254\353\235\274\354\236\204 \354\227\260\352\265\254\354\236\220\354\230\200\353\215\230 \352\261\264\354\227\220 \353\214\200\355\225\230\354\227\254 (Hard).py" "b/YoonYn9915/greedy/2025-06-03-[\353\260\261\354\244\200]-#14698-\354\240\204\354\203\235\355\226\210\353\215\224\353\213\210 \354\212\254\353\235\274\354\236\204 \354\227\260\352\265\254\354\236\220\354\230\200\353\215\230 \352\261\264\354\227\220 \353\214\200\355\225\230\354\227\254 (Hard).py" new file mode 100644 index 0000000..0b524bb --- /dev/null +++ "b/YoonYn9915/greedy/2025-06-03-[\353\260\261\354\244\200]-#14698-\354\240\204\354\203\235\355\226\210\353\215\224\353\213\210 \354\212\254\353\235\274\354\236\204 \354\227\260\352\265\254\354\236\220\354\230\200\353\215\230 \352\261\264\354\227\220 \353\214\200\355\225\230\354\227\254 (Hard).py" @@ -0,0 +1,54 @@ +''' +요구사항 +1. A만큼의 에너지를 가진 슬라임과 B만큼의 에너지를 가진 슬라임을 합성하려면 A × B 만큼의 에너지 필요. +2. N마리의 슬라임들을 적절히 합성해서 1마리의 슬라임으로 만들때, 필요한 에너지 값을 모두 곱한 값을 최소로 만든다. +3. 슬라임을 모두 합성했을 때 청구될 비용의 최솟값을 1, 000, 000, 007로 나눈 나머지를 출력한다. 에너지가 전혀 필요하지 않은 경우엔 1 을 출력한다. + +1. 아이디어 +곱의 누적곱이 최소가 되게 하려면 곱셈의 결과가 최소가 나오게 해야 함. 즉 큰 수를 가장 적게 곱하고 +되도록 작은수 X 작은수 형태로 풀이. 따라서 각 경우마다 가장 작은 두 수를 그리디하게 뽑아서 곱하고 누적곱해주면 된다. + +2. 시간복잡도 +슬라임 수 N (1 ≤ N ≤ 60), i번째 슬라임의 에너지 Ci (2 ≤ Ci ≤ 2 × 1018)일때, +모든 테스트 케이스에 대한 N 의 총합이 1, 000, 000을 넘지 않음으로 테스트 케이스의 수는 최대 500,000. +즉 O(test_case * (N-1)) == O(500,000 * 59)로 만족. + +3. 구현 +3-1. 입력받기 +3-2. N-1번 가장 작은 두 수를 뽑는다. +3-3. 두 수를 곱하고 정답변수에 누적시킨다. +3-4. 곱한 두 슬라임을 제거하고, 새 슬라임을 추가한다. +3-5. 정답 출력 + +''' + + +import heapq +import sys + +inp = sys.stdin.readline +MOD = 1000000007 + +test_case = int(inp()) + +for _ in range(test_case): + N = int(inp()) + arr = list(map(int, inp().split())) + + if N == 1: + print(1) + continue + + hq = [] + for num in arr: + heapq.heappush(hq, num) + + result = 1 + while len(hq) > 1: + a = heapq.heappop(hq) + b = heapq.heappop(hq) + energy = a * b + result = (result * energy) % MOD + heapq.heappush(hq, energy) + + print(result) \ No newline at end of file From 2ccfa8abfe39576188f089122e5cb441d200d4b4 Mon Sep 17 00:00:00 2001 From: YoonYn9915 Date: Sat, 7 Jun 2025 22:40:29 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[BOJ]=20=EB=B0=94=EB=8B=A5=20=EC=9E=A5?= =?UTF-8?q?=EC=8B=9D=20/=20=EC=8B=A4=EB=B2=84=204=20/=2050=EB=B6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acmicpc.net/problem/1388 --- ...4\353\213\245 \354\236\245\354\213\235.py" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 "YoonYn9915/Graph/2025-06-07-[\353\260\261\354\244\200]-#1388-\353\260\224\353\213\245 \354\236\245\354\213\235.py" diff --git "a/YoonYn9915/Graph/2025-06-07-[\353\260\261\354\244\200]-#1388-\353\260\224\353\213\245 \354\236\245\354\213\235.py" "b/YoonYn9915/Graph/2025-06-07-[\353\260\261\354\244\200]-#1388-\353\260\224\353\213\245 \354\236\245\354\213\235.py" new file mode 100644 index 0000000..5bad1e8 --- /dev/null +++ "b/YoonYn9915/Graph/2025-06-07-[\353\260\261\354\244\200]-#1388-\353\260\224\353\213\245 \354\236\245\354\213\235.py" @@ -0,0 +1,27 @@ + +def dfs(x, y): + if graph[x][y] == '-': + graph[x][y] = 1 + for _y in [1, -1]: + Y = y + _y + if (Y > 0 and Y < m) and graph[x][Y] == '-': + dfs(x, Y) + if graph[x][y] == '|': + graph[x][y] = 1 + for _x in [1, -1]: + X = x + _x + if (X > 0 and X < n) and graph[X][y] == '|': + dfs(X, y) + + +n, m = map(int, input().split()) +graph = [] +for _ in range(n): + graph.append(list(input())) + +count = 0 +for i in range(n): + for j in range(m): + if graph[i][j] == '-' or graph[i][j] == '|': + dfs(i, j) + count += 1 From a96d45661612a47822bf3c59084f6d1c23892b1d Mon Sep 17 00:00:00 2001 From: YoonYn9915 Date: Sat, 7 Jun 2025 23:05:12 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[BOJ]=20=EC=B9=B4=EB=93=9C=20=EC=A0=95?= =?UTF-8?q?=EB=A0=AC=ED=95=98=EA=B8=B0=20/=20=EA=B3=A8=EB=93=9C=204=20/=20?= =?UTF-8?q?30=EB=B6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acmicpc.net/problem/1715 --- ...25\353\240\254\355\225\230\352\270\260.py" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 "YoonYn9915/2025-06-07-[\353\260\261\354\244\200]-#1715-\354\271\264\353\223\234 \354\240\225\353\240\254\355\225\230\352\270\260.py" diff --git "a/YoonYn9915/2025-06-07-[\353\260\261\354\244\200]-#1715-\354\271\264\353\223\234 \354\240\225\353\240\254\355\225\230\352\270\260.py" "b/YoonYn9915/2025-06-07-[\353\260\261\354\244\200]-#1715-\354\271\264\353\223\234 \354\240\225\353\240\254\355\225\230\352\270\260.py" new file mode 100644 index 0000000..0786d4d --- /dev/null +++ "b/YoonYn9915/2025-06-07-[\353\260\261\354\244\200]-#1715-\354\271\264\353\223\234 \354\240\225\353\240\254\355\225\230\352\270\260.py" @@ -0,0 +1,44 @@ +''' +요구사항 +1. 매우 많은 숫자 카드 묶음이 책상 위에 놓여 있다. 이들을 두 묶음씩 골라 서로 합쳐나갈 때, 최소한 몇 번의 비교가 필요한지를 계산 + +1. 아이디어 +- A개, B개 두 묶음의 카드를 비교하는데 A+B번 비교해야 함. +- 비교한 횟수를 누적시켜 최소값을 찾아야 하므로 비교횟수가 가장 작은 것들부터 시작해야 함. +- A, B, C 3개의 카드 묶음이 있고 A < B < C라면, (A+B) + ((A+B) + C)가 최소값이다. + +2. 시간복잡도 +- (1 ≤ N ≤ 100,000) +- O(N + (N-1) * logN), 대략 O(NlogN)으로 시간복잡도 만족. + +3. 구현 +3.1 입력받기 +3.2 카드 묶음 정렬(우선순위 큐에 저장) +3.3 가장 카드가 적은 묶음 2개를 뽑아 더한 값을 정답변수에 누적시키고 다시 우선순위큐에 저장 +3.4 우선순위 큐가 하나 남을때까지 반복 +3.5 정답 출력 +''' + +import sys +import heapq + +inp = sys.stdin.readline + +N = int(inp().strip()) +arr = [] +answer = 0 + +for _ in range(N): + arr.append(int(inp().strip())) + +# 매번 최소 카드 묶음 2개를 뽑기 위해 heap 사용 +heapq.heapify(arr) + +while len(arr) > 1: + min1 = heapq.heappop(arr) + min2 = heapq.heappop(arr) + + answer += min1 + min2 + heapq.heappush(arr, min1 + min2) + +print(answer)