๐Algorithm/๐ฅBaekjoon
[Baekjoon] 26215_๋ ์น์ฐ๊ธฐ
๋ฟ์ผ._.
2023. 11. 6. 10:21
๋ฌธ์ (์ถ์ฒ: 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 : ๋ชจ๋ ์ง ์์ ๋์ ์น์ฐ๋ ๋ฐ ์๊ฐ
์ฐ์ ์์ ํ์ ๋์ ์์ด ๋ง์ ์์๋๋ก ์ ๋ ฌํ๋ค.
๋์ ์์ด ๊ฐ์ฅ ๋ง์ ์ง์ ๋ฝ๊ณ ์ง์ด ํ๋๋ฐ์ ๋จ์ง ์์๋ค๋ฉด ๊ทธ ๋์ ์์ด ์๊ฐ์ด ๋๋ค. ๋ค๋ฅธ ์ง๋ ์๋ค๋ฉด ํ ์ง์ ๋ ๋ฝ์ ๋ ๋ฒ์งธ ์ง ๋์ ์๋งํผ ๋์ ์น์ฐ๊ณ ๋ค์ ์ฐ์ ์์ ํ์ ๋ฃ๋๋ค.
์ด ๊ณผ์ ์ ๋ฐ๋ณตํ๋ฉด ๋ชจ๋ ์ง ์์ ๋์ ์น์ฐ๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ๊ตฌํ ์ ์๋ค.