https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV134DPqAA8CFAYh

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

<풀이>

1. 각 빌딩들의 높이를 입력받는다.

2. 각 빌딩마다 조망에 영향을 주는 4개의 빌딩(왼쪽 두 개, 오른쪽 두 개)의 빌딩 중 가장 높은 빌딩 높이를 구한다.

3. 만약 가장 높은 빌딩의 높이보다 현재 빌딩의 높이가 더 높다면, 높이의 차이만큼 조망권이 확보되는 세대가 생긴다.

4. 위의 세대들을 합하여 결과를 출력한다.

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
#include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
 
    int test_case;
    int T;
 
    T = 10;
 
    for (test_case = 1; test_case <= T; test_case++) {
 
        //사용할 변수 초기화
        int N;
        int building[1000];
        int res = 0;
 
        //입력
        cin >> N;
        for (int i = 0; i < N; i++) {
            cin >> building[i];
        }
 
        //각 빌딩마다 탐색
        for (int i = 2; i < N - 2; i++) {
 
            //빌딩에 영향을 주는 4개 빌딩 중 가장 높은 높이
            int maxHeight = max(max(building[i - 2], building[i - 1]), max(building[i + 1], building[i + 2]));
 
            //빌딩이 가장 높은 높이보다 높다면, 결과 저장
            if (maxHeight < building[i]) {
                res += building[i] - maxHeight;
            }
        }
 
        //출력
        cout << "#" << test_case << " " << res << "\n";
    }
}
 

 

구현에 대해 알아볼 수 있는 문제였습니다.

+ Recent posts