Lewis's Tech Keep
[BOJ][14247] 나무 자르기 - JAVA 본문
링크 : https://www.acmicpc.net/problem/14247
14247번: 나무 자르기
영선이는 나무꾼으로 나무를 구하러 오전에 산에 오른다. 산에는 n개의 나무가 있는데, 영선이는 하루에 한 나무씩 n일 산에 오르며 나무를 잘라갈 것이다. 하지만 이 산은 영험한 기운이 있어
www.acmicpc.net
- 풀이
- 탐욕법 문제
- 현재 최선의 방법을 구하는 문제
- 성장 가능성만 따져서 성장 가능성이 가장 낮은 것 부터 잘라낸다
더보기
import java.util.*;
public class Solution {
static class Tree {
int baseNum;
int growNum;
public Tree(int baseNum, int growNum) {
this.baseNum = baseNum;
this.growNum = growNum;
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
long answer = 0;
int n = sc.nextInt();
int[] Hi = new int[n];
int[] Ai = new int[n];
PriorityQueue<Tree> pq = new PriorityQueue<Tree>((a, b) -> a.growNum - b.growNum);
for (int i = 0; i < n; i++) {
Hi[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
Ai[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
pq.add(new Tree(Hi[i], Ai[i]));
}
int growCount = 0;
while (!pq.isEmpty()) {
Tree ct = pq.poll();
int treeHeight = ct.baseNum + (ct.growNum * growCount++);
answer += treeHeight;
}
System.out.println(answer);
}
}
'Java > 알고리즘' 카테고리의 다른 글
[BOJ][2304] 창고 다각형 - JAVA (0) | 2021.06.26 |
---|---|
[프로그래머스] 경주로 여행 - JAVA (0) | 2021.06.26 |
[프로그래머스] 합승 택시 요금 - Java (0) | 2021.06.25 |
[프로그래머스] 불량 사용자 (0) | 2021.06.24 |
[BOJ][16947] 서울 지하철 2호선 - JAVA (0) | 2021.06.24 |
Comments