๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 3211_kino

๋ฟŒ์•ผ._. 2025. 4. 2. 17:29
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/3211)

< kino >

 

๋ฌธ์ œ ํ’€์ด 

 

์นœ๊ตฌ์˜ ์š”์ฒญ์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์˜ํ™”๊ด€์— ๊ฐˆ ์ตœ์†Œํ•œ์˜ ์‚ฌ๋žŒ ์ˆ˜๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

 

my solution (Java)

 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.Arrays;
 
 public class _3211_ { // kino
 
 	public static void main(String[] args) throws IOException {
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 
 		int N = Integer.parseInt(bf.readLine());
 
 		int arr[] = new int[N];
 		for (int i = 0; i < N; i++) {
 			arr[i] = Integer.parseInt(bf.readLine());
 		}
 
 		Arrays.sort(arr);
 
 		int result = 0;
 		int max = arr[0];
 		while (result < N) {
 			result += (max - result);
 			max = Math.max(max, arr[result]);
 
 			if (result >= max) {
 				break;
 			}
 		}
 		System.out.println(result + 1);
 	}
 }
๋ณ€์ˆ˜)
N : ์นœ๊ตฌ ์ˆ˜
arr : ์นœ๊ตฌ์˜ ์š”์ฒญ
result : ์˜ํ™”๊ด€์— ๊ฐˆ ์ตœ์†Œํ•œ์˜ ์‚ฌ๋žŒ ์ˆ˜
max : ์นœ๊ตฌ์˜ ์š”์ฒญ ์ตœ๋Œ“๊ฐ’

 

์นœ๊ตฌ ์ˆ˜ N์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. N๋งŒํผ ์นœ๊ตฌ์˜ ์š”์ฒญ์„ ์ž…๋ ฅ๋ฐ›์•„ ๋ฐฐ์—ด arr์— ์ €์žฅํ•œ๋‹ค. arr์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. ๋ฐฐ์—ด์„ ์•ž์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉฐ ์นœ๊ตฌ์˜ ์š”์ฒญ๊ฐ’์— ๋งž๋Š” ์ตœ์†Œํ•œ์˜ ์‚ฌ๋žŒ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

 

result๋ฅผ 0, max๋ฅผ arr[0]์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ ํ›„ result๊ฐ€ N๋ณด๋‹ค ์ž‘์„ ๋•Œ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

1) max๊ฐ’๋งŒํผ ์นœ๊ตฌ๋ฅผ ๋ฐ๋ ค๊ฐ€์•ผ ํ•˜๋ฏ€๋กœ result + (max-result)

2) max๋ฅผ arr[result] ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธ

3) ๋งŒ์•ฝ result >= max๋ผ๋ฉด ๋ชจ๋“  ์‚ฌ๋žŒ์˜ ์š”์ฒญ์ด ์ถฉ์กฑ๋œ ์ตœ์†Œํ•œ์˜ ์‚ฌ๋žŒ ์ˆ˜๋ฅผ ๊ตฌํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ์ข…๋ฃŒ

์ตœ์ข… result๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.



 

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

[Baekjoon] 6138_Exploration  (0) 2025.04.04
[Baekjoon] 15237_Cipher  (0) 2025.04.03
[Baekjoon] 14184_IOI 2017 Logo  (0) 2025.04.01
[Baekjoon] 5614_ๅ•้กŒ 3  (0) 2025.03.31
[Baekjoon] 24755_Election Paradox  (0) 2025.03.27