-
[KOITP] 동굴자료구조 및 알고리즘/문제풀이 2016. 12. 19. 09:36
출처 : http://koitp.org/problem/COCI_2007_FIREFLY/read/
시간 제한 메모리 제한 제출 횟수 정답 횟수 (비율) 정답자 수 2.0 초 512 MB 1486 340 (23%) 284 < Comment >
발상이 무척 어려웠고, 처음에 설명을 듣고도 잘 이해가 안 갔습니다. 왜 입력을 받고 더하고 하는지 잘 이해가 안 되었는데, 찬찬히 살펴보니 그다지 어려운 내용은 아니었지만, 개인적으로는 배열을 두개로 나누는 것이 편해서 나누어 코딩했습니다. 각각 배열의 의미는 처음에 입력할 때는 각 배열 index를 높이로 가지는 배열이지만, 이후에 작업을 하고 난 후에는 index 높이로 지나갈 경우 부딪히는 종유석/석순의 수로 보게 됩니다. 시간제한 때문에 전체탐색은 일단 배제되며, 이렇게 종유석과 석순을 각 높이로 진행할 경우로 계산합니다. 석순같은 경우에는 그대로 해석하면 되지만 종유석 같은 경우에는 위에서부터 진행되기 때문에 석순 입장에서 i 높이라면 종유석에서는 H - i + 1 인덱스로 지나갔다고 볼 수 있기 때문에, 둘을 합친 값들 중에 최소값을 구하면 결과가 도출됩니다.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Arrays; import java.util.StringTokenizer; public class Cave { public static int N = 0; public static int H = 0; public static int bot[] = null; public static int top[] = null; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st = new StringTokenizer(br.readLine()); N = Integer.parseInt(st.nextToken()); H = Integer.parseInt(st.nextToken()); bot = new int[H + 1]; top = new int[H + 1]; int curr = 0; for (int i = 0 ; i < N ; i++) { curr = Integer.parseInt(br.readLine()); if (i % 2 == 0) { bot[curr]++; } else { top[curr]++; } } for (int i = H ; i > 0 ; i--) { bot[i - 1] += bot[i]; top[i - 1] += top[i]; } int result[] = new int[H+1]; int min = Integer.MAX_VALUE; for (int i = 1 ; i <= H ; i++) { result[i] = bot[i] + top[H - i + 1]; min = Math.min(min, result[i]); } int count = 0; for (int i = 1 ; i <= H ; i++) { if (min == result[i]) { count++; } } bw.write("" + min + " " + count); bw.flush(); bw.close(); } }