greedy

https://www.acmicpc.net/problem/21943 코드from itertools import combinationsn = int(input())x = list(map(int, input().split()))plus, mul = map(int, input().split())ans = 0def dfs(picked_cnt, remain_x, total): global ans if picked_cnt == 1: # 마지막 덩어리 ans = max(ans, total*sum(remain_x)) return # 하나의 덩어리 고르기 for i in range(1, len(remain_x)-picked_cnt+2): for comb in ..
https://www.acmicpc.net/problem/2012 코드import sysinput = sys.stdin.readlinen = int(input())ranks = sorted(list(int(input()) for _ in range(n)))ans = 0for i in range(1, n+1): ans += abs(ranks[i-1]-i)print(ans) 무조건 예상 등수를 높게 적어낸 학생부터 높은 등수를 주어야 전체 불만도의 총합이 작아진다. 그러므로 예상 등수를 오름차순 정렬하여 그대로 등수를 부여하고 불만도를 구하면 된다.
https://www.acmicpc.net/problem/20115 코드n = int(input())drinks = list(map(int, input().split()))drinks.sort()ans = drinks[-1]for i in range(len(drinks)-1): ans += (drinks[i]/2)print(ans) 처음에는 남은 음료들 중 가장 양이 적은 2개를 골라서 합쳐야 하는줄 알았는데, 아니었다. 매 순서에 2로 나누어지는 음료들을 최대한 작게 해주어야 하는데, 이렇게 하면 그럴 수 없다.예를 들어, 음료 5개가 가장 작은 것부터 순서대로 a, b, c, d, e 이렇게 있다고 하면 원래 생각한 방법대로면 처음에 a, b를 뽑아서 (a/2)+b를 만들게 된다. 그 다음 순서..
https://www.acmicpc.net/problem/1493 코드import sysinput = sys.stdin.readlinel, w, h = map(int, input().split())n = int(input())cubes = []for _ in range(n): a, b = map(int, input().split()) cubes.append([2**a, b])def dc(l, w, h, idx): # 분할정복 if idx == -1: # 큐브 부족 print(-1) exit() res = 0 # idx번째 큐브의 필요 개수 need = (l//cubes[idx][0])*(w//cubes[idx][0]) * (h//cubes[..
https://www.acmicpc.net/problem/17451 코드import sysinput = sys.stdin.readlinen = int(input())v = list(map(int, input().split()))ans = v[-1]for i in range(n-2, -1, -1): if ans % v[i] != 0: ans = (ans//v[i]+1)*v[i]print(ans) 처음에 Parametric Search로 풀이했다가 시간 초과가 발생하였는데, Parametric Search 알고리즘만 하면 시간 복잡도가 O(logN)인데 조건 검사할 때마다 O(N)이 추가로 필요해서 그런 것 같다. 그래서 다시 살펴보니 Greedy로 풀이할 수 있을 것 같아서 바꾸었다. 행..
딜레이레이
'greedy' 태그의 글 목록 (2 Page)