알고리즘

백준 19638번 센티와 마법의 뿅망치 (JAVA)

박카스마시며코딩 2022. 7. 20. 16:36

https://www.acmicpc.net/problem/19638

 

19638번: 센티와 마법의 뿅망치

마법의 뿅망치를 센티의 전략대로 이용하여 거인의 나라의 모든 거인이 센티보다 키가 작도록 할 수 있는 경우, 첫 번째 줄에 YES를 출력하고, 두 번째 줄에 마법의 뿅망치를 최소로 사용한 횟수

www.acmicpc.net

 

저는 해당 문제를 우선순위 큐를 이용해 문제를 해결하였습니다.

우선순위 큐를 이용해 가장 키가 큰 칭구를 가져오고 이를 반으로 줄였습니다.

1이하로는 안 떨어지기 때문에 1과 비교하여 1보다 작다면 1을 넣도록 구현하였습니다.

 

package BOJ.greedy;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_19638 {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        Function<String,Integer> stoi = Integer::parseInt;
        int n = stoi.apply(st.nextToken());
        int h = stoi.apply(st.nextToken());
        int t = stoi.apply(st.nextToken());
        PriorityQueue<Integer> heights = new PriorityQueue<>(Collections.reverseOrder());
        for(int i =  0 ; i < n ; i++){
            int num = stoi.apply(br.readLine());
            heights.add(num);
        }
        int cnt = 0;
        while(h <= heights.peek() && cnt < t){
             int height = heights.poll();
             height = Math.max(1, height/2);
             heights.add(height);
             cnt++;
        }
        if(h > heights.peek() && cnt <= t){
            System.out.println("YES");
            System.out.println(cnt);
        }else{
            System.out.println("NO");
            System.out.println(heights.peek());
        }
    }
}