Lewis's Tech Keep

[BOJ][14247] 나무 자르기 - JAVA 본문

Java/알고리즘

[BOJ][14247] 나무 자르기 - JAVA

Lewis Seo 2021. 6. 26. 00:41

링크 : 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);
    }
}
Comments