๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 26215_๋ˆˆ ์น˜์šฐ๊ธฐ

๋ฟŒ์•ผ._. 2023. 11. 6. 10:21

Silver III

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/26215)

< ๋ˆˆ ์น˜์šฐ๊ธฐ >

 

๋ฌธ์ œ ํ’€์ด 

 

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ˆˆ์˜ ์–‘์ด ํฐ ์ˆœ์„œ๋Œ€๋กœ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋‘”๋‹ค. ๋ˆˆ์˜ ์–‘์ด ํฐ ์ง‘๋ถ€ํ„ฐ ๋ˆˆ์„ ์น˜์šด๋‹ค.

 

 

 my solution (Java)

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

public class _26215_ { // ๋ˆˆ ์น˜์šฐ๊ธฐ

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		int n = Integer.parseInt(bf.readLine());

		st = new StringTokenizer(bf.readLine());
		PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());

		for (int i = 0; i < n; i++) {
			queue.add(Integer.parseInt(st.nextToken()));
		}

		int result = 0;
		while (!queue.isEmpty()) {
			int a = queue.poll();

			if (queue.isEmpty()) {
				result += a;
				break;
			}
			int b = queue.poll();
			result += b;
			queue.add(a - b);
		}

		if (result > 1440)
			System.out.println(-1);
		else
			System.out.println(result);
	}
}

 

Main

๋ณ€์ˆ˜)
n : ์ง‘์˜ ์ˆ˜
queue : ์šฐ์„ ์ˆœ์œ„ ํ (๋‚ด๋ฆผ์ฐจ์ˆœ)
result : ๋ชจ๋“  ์ง‘ ์•ž์˜ ๋ˆˆ์„ ์น˜์šฐ๋Š” ๋ฐ ์‹œ๊ฐ„

 

์šฐ์„ ์ˆœ์œ„ ํ์— ๋ˆˆ์˜ ์–‘์ด ๋งŽ์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•œ๋‹ค.

 

๋ˆˆ์˜ ์–‘์ด ๊ฐ€์žฅ ๋งŽ์€ ์ง‘์„ ๋ฝ‘๊ณ  ์ง‘์ด ํ•˜๋‚˜๋ฐ–์— ๋‚จ์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ทธ ๋ˆˆ์˜ ์–‘์ด ์‹œ๊ฐ„์ด ๋œ๋‹ค. ๋‹ค๋ฅธ ์ง‘๋„ ์žˆ๋‹ค๋ฉด ํ•œ ์ง‘์„ ๋” ๋ฝ‘์•„ ๋‘ ๋ฒˆ์งธ ์ง‘ ๋ˆˆ์˜ ์–‘๋งŒํผ ๋ˆˆ์„ ์น˜์šฐ๊ณ  ๋‹ค์‹œ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋„ฃ๋Š”๋‹ค.

์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉด ๋ชจ๋“  ์ง‘ ์•ž์˜ ๋ˆˆ์„ ์น˜์šฐ๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.