공부하는 히욤이

1206. [S/W 문제해결 기본] 1일차 - View 본문

Algorithm/SW Expert Academy

1206. [S/W 문제해결 기본] 1일차 - View

히욤이 2019. 2. 21. 00:11

1206. [S/W 문제해결 기본] 1일차 - View

문제의 저작권은 SW Expert에 있습니다 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
 
public class Solution {
 
    public static void main(String[] args) throws IOException {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader br = new BufferedReader(new FileReader("input.txt"));
        for (int test_case = 1; test_case <= 10; test_case++) {
            int len = Integer.parseInt(br.readLine());//테스트케이스의 길이
            int[] arr = new int[len]; //건물 배열
 
            StringTokenizer st;
            st = new StringTokenizer(br.readLine());
 
            for (int i = 0; i < len; i++) { //배열에 층수 삽입
                arr[i] = Integer.parseInt(st.nextToken());
            }
 
            int answer = 0//결과 
 
            for (int i = (0+2); i < (arr.length-2); i++) { //맨 앞 2칸과 맨 뒤 2칸은 00이기 때문에 빼고 돌림
                /*조망권이 확보되는 왼쪽칸과 오른쪽 칸의 세대의 수 */                
                int le1 = arr[i] - arr[i-1]; //왼쪽 1칸 : 현재 칸 - 왼쪽 1칸
                int le2 = arr[i] - arr[i-2]; //왼쪽 2칸 : 현재 칸 - 왼쪽 2칸
                int ri1 = arr[i] - arr[i+1]; //오른쪽 1칸 : 현재 칸 - 오른쪽 1칸
                int ri2 = arr[i] - arr[i+2]; //오른쪽 2칸 : 현재칸 - 오른쪽 2칸
 
                if (le1<0 || le2<0 || ri1<0 || ri2<0) { //하나라도 -가 되면 조망권 X
                    continue;
                }
                int view =  + Math.min(Math.min(le1, le2),Math.min((ri1),(ri2))); //최소 조망권의 수
                answer = answer +view;
            }
            System.out.println("#"+test_case + " " + answer);
        }
    }
}
 
cs