๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 5545_์ตœ๊ณ ์˜ ํ”ผ์ž

๋ฟŒ์•ผ._. 2023. 11. 3. 09:25

Silver III

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

< ์ตœ๊ณ ์˜ ํ”ผ์ž >

 

๋ฌธ์ œ ํ’€์ด 

 

ํ† ํ•‘์„ ์„ ํƒํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, ํ† ํ•‘์„ ํ•˜๋‚˜์”ฉ ์„ ํƒํ•œ ๊ฒฝ์šฐ๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ์ตœ๊ณ ์˜ ํ”ผ์ž์˜ 1์› ๋‹น ์—ด๋Ÿ‰์„ ๊ตฌํ•œ๋‹ค.

 

 

 my solution (Java)

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

public class _5545_ { // ์ตœ๊ณ ์˜ ํ”ผ์ž

	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());
		int a = Integer.parseInt(st.nextToken());
		int b = Integer.parseInt(st.nextToken());
		int c = Integer.parseInt(bf.readLine());

		Integer arr[] = new Integer[n];
		for (int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(bf.readLine());
		}

		Arrays.sort(arr, Collections.reverseOrder());

		int money = a;
		int kal = c;
		int result = kal / money;

		for (int i = 0; i < n; i++) {
			money += b;
			kal += arr[i];
			if (kal / money > result) {
				result = kal / money;
			}
		}
		System.out.println(result);
	}
}

 

Main

๋ณ€์ˆ˜)
n : ํ† ํ•‘์˜ ์ข…๋ฅ˜์˜ ์ˆ˜
a, b : ๋„์šฐ์˜ ๊ฐ€๊ฒฉ, ํ† ํ•‘์˜ ๊ฐ€๊ฒฉ
c : ๋„์šฐ์˜ ์—ด๋Ÿ‰
arr : ๋„ํ•‘์˜ ์—ด๋Ÿ‰
money, cal, result : ๊ฐ€๊ฒฉ, ์นผ๋กœ๋ฆฌ, 1์› ๋‹น ์นผ๋กœ๋ฆฌ

 

ํ† ํ•‘์˜ ์—ด๋Ÿ‰์„ ์ž…๋ ฅ๋ฐ›์•„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. ํ† ํ•‘์„ ํ•˜๋‚˜๋„ ์„ ํƒํ•˜์ง€ ์•Š์•˜์„ ๋•Œ๋ฅผ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. ๊ทธ ํ›„์— ํ† ํ•‘์„ ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ์˜ ์นผ๋กœ๋ฆฌ๋ฅผ ๊ตฌํ•ด ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•œ๋‹ค.