๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1758_์•Œ๋ฐ”์ƒ ๊ฐ•ํ˜ธ

๋ฟŒ์•ผ._. 2023. 9. 28. 00:13

Silver IV

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

< ์•Œ๋ฐ”์ƒ ๊ฐ•ํ˜ธ >

 

๋ฌธ์ œ ํ’€์ด 

 

ํŒ์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํŒ์ด ๋งŽ์€ ์ˆœ์„œ๋Œ€๋กœ ์†๋‹˜์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ๋ฉด ๋œ๋‹ค.

 

 my solution (Java)

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

public class _1758_ { // ์•Œ๋ฐ”์ƒ ๊ฐ•ํ˜ธ

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

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

		PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
		for (int i = 0; i < n; i++) {
			queue.add(Integer.parseInt(bf.readLine()));
		}

		long result = 0;
		int idx = 1;

		while (!queue.isEmpty()) {
			int num = queue.poll() - (idx++ - 1);
			if (num > 0)
				result += num;
		}

		System.out.println(result);
	}
}

 

Main

๋ณ€์ˆ˜)
n : ์‚ฌ๋žŒ ์ˆ˜
queue : ์šฐ์„ ์ˆœ์œ„ ํ
idx : ์†๋‹˜ ์ˆœ์„œ
result : ํŒ์˜ ์ตœ๋Œ“๊ฐ’

 

- ์‚ฌ๋žŒ ์ˆ˜(n) ์ž…๋ ฅ

- ๊ฐ ์‚ฌ๋žŒ์ด ์ฃผ๋ ค๊ณ  ํ•˜๋Š” ํŒ์„ ์ž…๋ ฅ๋ฐ›์•„ ์šฐ์„ ์ˆœ์œ„ ํ์— ์ €์žฅ

- ์šฐ์„ ์ˆœ์œ„ ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

: ๋ˆ - (๋ฐ›์€ ๋“ฑ์ˆ˜ idx - 1) ๊ณ„์‚ฐ

: ๊ณ„์‚ฐํ•œ ๊ฐ’์ด ์Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด result์— ๋”ํ•จ

- ํŒ์˜ ์ตœ๋Œ“๊ฐ’(result) ์ถœ๋ ฅ