๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 2865_๋‚˜๋Š” ์œ„๋Œ€ํ•œ ์Šˆํผ์Šคํƒ€K

๋ฟŒ์•ผ._. 2024. 1. 26. 11:30

Silver IV

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

< ๋‚˜๋Š” ์œ„๋Œ€ํ•œ ์Šˆํผ์Šคํƒ€K >

 

๋ฌธ์ œ ํ’€์ด 

 

๊ฐ ์žฅ๋ฅด๋งˆ๋‹ค ์ฐธ๊ฐ€์ž์˜ ๋Šฅ๋ ฅ์„ ๋ณด๋ฉด์„œ ๊ฐ ์ฐธ๊ฐ€์ž๋งˆ๋‹ค ๋Šฅ๋ ฅ์ด ๋†’์€ ๊ฐ’์„ ์ฐพ๋Š”๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class _2865_ { // ๋‚˜๋Š” ์œ„๋Œ€ํ•œ ์Šˆํผ์Šคํƒ€K

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

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());

		double[] arr = new double[n + 1];

		for (int i = 0; i < m; i++) {
			st = new StringTokenizer(bf.readLine());
			for (int j = 0; j < n; j++) {
				int num = Integer.parseInt(st.nextToken());
				double score = Double.parseDouble(st.nextToken());

				if (arr[num] < score) {
					arr[num] = score;
				}
			}
		}
		Arrays.sort(arr);

		double result = 0.0;
		for (int i = n; i > n - k; i--) {
			result += arr[i];
		}
		System.out.println(String.format("%.1f", result));

	}
}
๋ณ€์ˆ˜)
n, m, k : ์žฅ๋ฅด์˜ ์ˆ˜, ์‚ฌ๋žŒ ์ˆ˜, ๋ณธ์„ ์— ๋‚˜๊ฐ€๋Š” ์‚ฌ๋žŒ ์ˆ˜
arr : ๊ฐ ์ฐธ๊ฐ€์ž์˜ ์ตœ๋Œ€ ๋Šฅ๋ ฅ
num, score : ์ฐธ๊ฐ€์ž ๋ฒˆํ˜ธ, ์ฐธ๊ฐ€์ž์˜ ๋Šฅ๋ ฅ
result : ์ฐธ๊ฐ€์ž์˜ ๋Šฅ๋ ฅ์˜ ํ•ฉ

 

์žฅ๋ฅด์˜ ์ˆ˜, ์‚ฌ๋žŒ์˜ ์ˆ˜, ๋ณธ์„ ์— ๋‚˜๊ฐ€๋Š” ์‚ฌ๋žŒ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

๊ฐ ์žฅ๋ฅด๋งˆ๋‹ค ์ฐธ๊ฐ€์ž์˜ ๋ฒˆํ˜ธ์™€ ์ฐธ๊ฐ€์ž์˜ ์žฅ๋ฅด์— ๋Œ€ํ•œ ๋Šฅ๋ ฅ์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉฐ ๊ฐ ์ฐธ๊ฐ€์ž์˜ ์ตœ๋Œ€ ๋Šฅ๋ ฅ ๊ฐ’์„ ์ฐพ๋Š”๋‹ค.

์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ›„ ๋’ค์—์„œ๋ถ€ํ„ฐ k๋ช…์˜ ๋Šฅ๋ ฅ ํ•ฉ์„ ๊ตฌํ•˜์—ฌ ์†Œ์ˆ˜์  ์ฒซ์งธ ์ž๋ฆฌ๊นŒ์ง€ ๋ฐ˜์˜ฌ๋ฆผํ•ด ์ถœ๋ ฅํ•œ๋‹ค.