티스토리 뷰

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

 

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

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

www.acmicpc.net

 

풀이

 

1) PriorityQueue 로 거인들의 키를 담는다. 이때 큰 순서가 되기위해 Collections.reverseOrder()를 쓴다.

2) pq에서 하나씩 뽑는다.

2-1) 센티보다 크면 뿅망치로 때리고 다시 집어 넣는다. (이 때, 키가 1인 경우는 봐준다..)

2-2) 센티보다 작으면 그냥 넣어준다.

3) 2) 를 뿅망치 사용 횟수 T 만큼 반복한다.

4) 만약 pq.peek() 가 센티의 키보다 작으면 "YES", 크면 "NO"를 출력한다.

 

 

주의사항

 

1) ⌊ 뿅망치에 맞은 사람의 키 / 2 ⌋ <- 여기 기호가 내림이라는 걸 처음알게 되었다.

 

package com.baekJoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class BJ_S1_19638_센티와마법의뿅망치 {
	static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer tokens;
	static int N,H,T;
	static PriorityQueue<Integer> giant = new PriorityQueue<>(Collections.reverseOrder());
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		input = new BufferedReader(new StringReader(src));
		tokens = new StringTokenizer(input.readLine());
		N = Integer.parseInt(tokens.nextToken());
		H = Integer.parseInt(tokens.nextToken());
		T = Integer.parseInt(tokens.nextToken());
		for(int n=0; n<N; n++) {
			giant.offer(Integer.parseInt(input.readLine()));
		}
		int tallest;
		int temp = T;
		while(temp>0) {
			tallest = giant.poll();
			if(tallest < H) {
				giant.offer(tallest);
				break;
			}else {
				if(tallest/2 == 0) {
					giant.offer(1);
					break;
				}else {
					giant.offer(tallest/2);
				}
			}
			temp--;
		}
		
		if(giant.peek() < H) {
			System.out.println("YES");
			System.out.println(T-temp);
		}else {
			System.out.println("NO");
			System.out.println(giant.peek());
		}
	}

	static String src =
			"1 1 100000\r\n" + 
			"1";
}

 

후기

 

⌊  ⌋ <- 기호를 처음 알게되었다. pq만 쓸줄 알면 쉽게 푸는 문제였다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함