๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 11785_Programming Contest Strategy

๋ฟŒ์•ผ._. 2024. 11. 6. 12:46
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/11785)

< Programming Contest Strategy >

 

๋ฌธ์ œ ํ’€์ด 

 

์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ L์ดํ•˜์ผ ๋•Œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ ์ˆ˜์™€ ํ•ด๊ฒฐ ์‹œ๊ฐ„, ์ด ํ•ด๊ฒฐ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class _11785_ { // Programming Contest Strategy
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st;
		int T = Integer.parseInt(bf.readLine());
		for (int i = 0; i < T; i++) {
			st = new StringTokenizer(bf.readLine());
			int N = Integer.parseInt(st.nextToken());
			int L = Integer.parseInt(st.nextToken());
			int arr[] = new int[N];
			st = new StringTokenizer(bf.readLine());
			for (int j = 0; j < N; j++) {
				arr[j] = Integer.parseInt(st.nextToken());
			}
			Arrays.sort(arr);
			int num = 0, time = 0, total = 0;
			for (int j = 0; j < N; j++) {
				if (time + arr[j] <= L) {
					num += 1;
					time += arr[j];
					total += time;
				} else {
					break;
				}
			}
			bw.write("Case " + (i + 1) + ": " + num + " " + time + " " + total + "\n");
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
T : ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜
N, L : ๋ฌธ์ œ ์ˆ˜, ์‹œ๊ฐ„
arr : ๋ฌธ์ œ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
num, time, total : ํ•ด๊ฒฐํ•œ ๋ฌธ์ œ ์ˆ˜, ํ•ด๊ฒฐ ์‹œ๊ฐ„, ์ด ๋ฌธ์ œ ํ•ด๊ฒฐ ์‹œ๊ฐ„

 

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜๋งŒํผ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

1) ๋ฌธ์ œ ์ˆ˜, ์‹œ๊ฐ„์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

2) ๋ฌธ์ œ ์ˆ˜๋งŒํผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅํ•œ๋‹ค.

3) arr ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ 

4) arr์„ ํƒ์ƒ‰ํ•˜๋ฉฐ ๋ฌธ์ œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ํ•ด๊ฒฐํ–ˆ์„ ๋•Œ L์ดํ•˜์ธ ๋ฌธ์ œ ์ˆ˜์™€ ๋ฌธ์ œ ํ•ด๊ฒฐ ์‹œ๊ฐ„, ์ด ๋ฌธ์ œ ํ•ด๊ฒฐ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค.

์ตœ์ข… num, time, total์„ ์ถœ๋ ฅํ•œ๋‹ค.



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 25099_Anagram  (0) 2024.11.08
[Baekjoon] 7587_Anagrams  (0) 2024.11.07
[Baekjoon] 7774_์ฝ˜์„ผํŠธ  (0) 2024.11.05
[Baekjoon] 11235_Polling  (0) 2024.11.04
[Baekjoon] 11609_Class Time  (0) 2024.11.01