๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/11969)
< Breed Counting >
๋ฌธ์ ํ์ด
ํน์ ๊ตฌ๊ฐ์ ํฌํจ๋ ๊ฐ ํ์ข ์ ์๊ฐ ๋ช ๋ง๋ฆฌ์ธ์ง ๊ตฌํ๋ ๊ฒ์ด๋ฏ๋ก ๋์ ํฉ์ ์ฌ์ฉํ๋ค.
์ฃผ์ด์ง ์์ ๋ฅผ ํธ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
6 3
2 -> [0,1,0]
1 -> [1,1,0]
1 -> [2,1,0]
3 -> [2,1,1]
2 -> [2,2,1]
1 -> [3,2,1]
1 6 => ์ ์ฒด ๊ตฌ๊ฐ์ด๋ฏ๋ก [3,2,1]
3 3 => [2,1,0] - [1,1,0] = [1,0,0]
2 4 => [2,1,1] - [0,1,0] = [2,0,1]
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.StringTokenizer;
public class _11969_ { // Breed Counting
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 = new StringTokenizer(bf.readLine());
int n = Integer.parseInt(st.nextToken());
int q = Integer.parseInt(st.nextToken());
int cow[][] = new int[n + 1][3];
for (int i = 1; i <= n; i++) {
int id = Integer.parseInt(bf.readLine()) - 1;
for (int j = 0; j < 3; j++) {
cow[i][j] = cow[i - 1][j];
}
cow[i][id] += 1;
}
for (int i = 0; i < q; i++) {
st = new StringTokenizer(bf.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
for (int j = 0; j < 3; j++) {
bw.write(cow[b][j] - cow[a - 1][j] + " ");
}
bw.write("\n");
}
bw.flush();
}
}
๋ณ์)
n : ์์ ์
q : ํน์ ๊ตฌ๊ฐ ๊ฐ์
cow : ์์ ๋์ ํฉ
a, b : ํน์ ๊ตฌ๊ฐ
์์ ์์ ํน์ ๊ตฌ๊ฐ ๊ฐ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ์์ ์๋งํผ ํ์ข ID๋ฅผ ์ ๋ ฅ๋ฐ์ cow ๋ฐฐ์ด์ ๋์ ํฉ์ ์ ์ฅํ๋ค. ํน์ ๊ตฌ๊ฐ ๊ฐ์๋งํผ ํน์ ๊ตฌ๊ฐ์ ์ ๋ ฅ๋ฐ์ ๋ฐฐ์ด [b] - [a-1] ๊ฐ์ ๊ตฌํด ์ถ๋ ฅํ๋ค.

'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Baekjoon] 3022_PRASE (0) | 2026.02.23 |
|---|---|
| [Baekjoon] 19622_ํ์์ค ๋ฐฐ์ 3 (0) | 2026.02.12 |
| [Baekjoon] 19621_ํ์์ค ๋ฐฐ์ 2 (0) | 2026.02.11 |
| [Baekjoon] 5953_Profits (0) | 2026.02.10 |
| [Baekjoon] 1699_์ ๊ณฑ์์ ํฉ (0) | 2026.02.09 |