๋ฌธ์ (์ถ์ฒ: 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 |