[Baekjoon] 7863_Very Simple Problem
๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/7863)
< Very Simple Problem >
๋ฌธ์ ํ์ด
๋ค์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋ฌธ์ ๋ฒํธ๋ฅผ ์ฐพ๋๋ค.
1. ๊ฐ์ฅ ์ฌ์ด ๋ฌธ์ ๋ก ๋ณธ ์ฌ์ฌ์์์ด ์ ์ฒด์ ๊ณผ๋ฐ์๋ณด๋ค ๋ง์์ผ ํ๋ค.
2. ๊ทธ ๋ฌธ์ ๋ฅผ ๊ฐ์ฅ ์ด๋ ค์ด ๋ฌธ์ ๋ก ๋ณธ ์ฌ์ฌ์์์ด ์์ด์ผ ํ๋ค.
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.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class _7863_ { // Very Simple Problem
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 p = Integer.parseInt(st.nextToken());
HashMap<Integer, Integer> simple = new HashMap<>();
Set<Integer> hard = new HashSet<>();
for (int i = 0; i < n; i++) {
st = new StringTokenizer(bf.readLine());
int arr[] = new int[p];
int min = 10001, max = 0;
for (int j = 0; j < p; j++) {
arr[j] = Integer.parseInt(st.nextToken());
min = Math.min(min, arr[j]);
max = Math.max(max, arr[j]);
}
for (int j = 0; j < p; j++) {
if (arr[j] == min) {
if (simple.containsKey(j + 1)) {
simple.replace(j + 1, simple.get(j + 1) + 1);
} else {
simple.put(j + 1, 1);
}
}
if (arr[j] == max) {
hard.add(j + 1);
}
}
}
ArrayList<Integer> list = new ArrayList<>(simple.keySet());
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return simple.get(o2) - simple.get(o1);
}
});
boolean flag = false;
ArrayList<Integer> result= new ArrayList<>();
for (int num : list) {
if (simple.get(num) > n / 2 && !hard.contains(num)) {
flag = true;
result.add(num);
}
}
if (!flag) {
bw.write("0");
}else {
Collections.sort(result);
for(int num : result) {
bw.write(num+" ");
}
}
bw.flush();
}
}
๋ณ์)
n, p : ์ฌ์ฌ์์ ์, ๋ฌธ์ ์
simple : HashMap <๊ฐ์ฅ ์ฌ์ด ๋ฌธ์ ๋ฒํธ, ๊ฐ์ฅ ์ฌ์ด ๋ฌธ์ ๋ก ๋ณธ ์ฌ์ฌ์์์ ์>
hard : ๊ฐ์ฅ ์ด๋ ค์ด ๋ฌธ์ ๋ฒํธ
arr : ๊ฐ ์ฌ์ฌ์์์ ๋ฌธ์ ์ ๋ํ ์ ์
min, max : ๊ฐ ์ฌ์ฌ์์์ ์ ์ ์ต์๊ฐ, ์ต๋๊ฐ
list : simple key -> ArrayList
flag : ์กฐ๊ฑด ๋ง์กฑ ์ฌ๋ถ
result : ์กฐ๊ฑด ๋ง์กฑํ๋ ๋ฌธ์ ๋ฒํธ
์ฌ์ฌ์์ ์์ ๋ฌธ์ ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ์ฌ์ฌ์์ ์๋งํผ ๋ค์ ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
1) ๊ฐ ๋ฌธ์ ์ ํด๋นํ๋ ์ ์๋ฅผ ์ ๋ ฅ๋ฐ์ผ๋ฉด์ ์ฌ์ฌ์์์ ์ ์ ์ต์๊ฐ๊ณผ ์ต๋๊ฐ์ ๊ตฌํ๋ค.
2) ์ต์๊ฐ์ ํด๋นํ๋ ๋ฌธ์ ๋ฒํธ๋ฅผ HashMap์ ์ ์ฅํ๊ณ , ์ต๋๊ฐ์ ํด๋นํ๋ ๋ฌธ์ ๋ฒํธ๋ฅผ Set์ ์ ์ฅํ๋ค.
HashMap์ key๊ฐ์ ArrayList๋ก ๋ณํ ํ ๊ฐ์ฅ ์ฌ์ด ๋ฌธ์ ๋ก ๋ง์ด ๋ณธ ๋ฌธ์ ์์ผ๋ก ์ ๋ ฌํ๋ค. ArrayList๋ฅผ ํ์ํ๋ฉด์ ์ฌ์ฌ์์ ๊ณผ๋ฐ์๋ณด๋ค ๋ง์ด ์ฌ์ด ๋ฌธ์ ๋ก ์ง์ ๋๊ณ ์ด๋ ค์ด ๋ฌธ์ ๋ก ์ง์ ๋ ์ ์๋ ๋ฌธ์ ๋ฒํธ๋ฅผ result ArrayList์ ์ ์ฅํ๋ค.
์ต์ข ArrayList๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ์ฌ ์ถ๋ ฅํ๋ค. ์ด๋, ์กฐ๊ฑด์ ๋ง๋ ๋ฌธ์ ๊ฐ ์๋ค๋ฉด "0"์ ์ถ๋ ฅํ๋ค.
