๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 18295_Ants

๋ฟŒ์•ผ._. 2026. 3. 19. 10:49
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/18295)

< Ants >

 

๋ฌธ์ œ ํ’€์ด 

 

์ž…๋ ฅ๊ฐ’ ์ค‘ 0 ์ด์ƒ์ด๊ณ  7์ž๋ฆฌ ์ดํ•˜๋ผ๋ฉด ArrayList์— ์ €์žฅํ•œ๋‹ค. ์ •๋ ฌ ํ›„ 0๋ถ€ํ„ฐ ํ™•์ธํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์— ์—†๋Š” ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

my solution (Java)

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

public class _18295_ { // Ants

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

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

		ArrayList<Integer> list = new ArrayList<>();

		for (int i = 0; i < n; i++) {
			String temp = bf.readLine();

			if (temp.length() >= 8) {
				continue;
			}

			int num = Integer.parseInt(temp);

			if (num >= 0) {
				list.add(num);
			}
		}

		Collections.sort(list);

		int result = 0;
		for (int x : list) {
			if (x != result) {
				break;
			}
			result += 1;
		}

		System.out.println(result);
	}
}
๋ณ€์ˆ˜)
n : ์ž…๋ ฅ ๊ฐœ์ˆ˜
list : ์ž…๋ ฅ๊ฐ’ ์ €์žฅํ•  ArrayList
result : ๋ฆฌ์ŠคํŠธ์— ์—†๋Š” ๊ฐ€์žฅ ์ž‘์€ ์ •์ˆ˜ 

 

์ž…๋ ฅ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ์ž…๋ ฅ ๊ฐœ์ˆ˜๋งŒํผ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„ 7์ž๋ฆฌ ์ดํ•˜์ด๊ณ  0 ์ด์ƒ์ด๋ผ๋ฉด ArrayList์— ์ €์žฅํ•œ๋‹ค. ์ด๋•Œ, 7์ž๋ฆฌ ์ดํ•˜์ธ์ง€ ํ™•์ธํ•˜๋Š” ์ด์œ ๋Š” n์˜ ๋ฒ”์œ„๊ฐ€ 0 ์ด์ƒ 1000000 ์ดํ•˜์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ArrayList๋ฅผ ์ •๋ ฌ ํ›„ ํƒ์ƒ‰ํ•˜๋ฉฐ 0๋ถ€ํ„ฐ ํ™•์ธํ–ˆ์„ ๋•Œ ๋ฆฌ์ŠคํŠธ์— ์—†๋Š” ๊ฐ€์žฅ ์ž‘์€ ์ •์ˆ˜๋ฅผ ์ฐพ์•„ ์ถœ๋ ฅํ•œ๋‹ค. 



 

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

[Baekjoon] 13243_Non-decreasing subsegment  (0) 2026.03.18
[Baekjoon] 6230_Buy One Get One Free  (0) 2026.03.16
[Baekjoon] 5059_Shopaholic  (0) 2026.03.09
[Baekjoon] 17550_Inquiry I  (0) 2026.03.04
[Baekjoon] 15465_Milk Measurement  (0) 2026.03.03