[SWEA/C++] 1206 View

Hanbi·2022년 11월 15일
0

Problem Solving

목록 보기
42/108
post-thumbnail

문제

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AV134DPqAA8CFAYh&categoryId=AV134DPqAA8CFAYh&categoryType=CODE&problemTitle=&orderBy=RECOMMEND_COUNT&selectCodeLang=CCPP&select-1=3&pageSize=10&pageIndex=1

풀이

처음에는 2*2 배열 만들어서 해당 부분 1로 채우고 조건 비교하는 거라고 생각했는데 더 효율적으로 짤 수 있었음
1차원 배열에 건물 높이만 저장하고 [i-2, i-1, i, i+1, i+2] 일 때, i가 max이고, max에서 나머지 4개 중 최댓값을 빼는 게 조망권 확보된 세대 수 !!

코드

#include<iostream>

using namespace std;

int main(int argc, char** argv) {
	int test_case;
	int T;
	
	//cin >> T;
	T = 10;
	for (test_case = 1; test_case <= T; ++test_case) {
		int h[1000] = { 0 };
		int num;
		int ans = 0;

		cin >> num;
		for (int i = 0; i < num; i++) {
			int tmp;
			cin >> tmp;
			h[i] = tmp;
		}

		for (int i = 0; i < num; i++) {
			if (i == 0 || i == 1 || i == i - 1 || i == i - 2)	continue;
			
			if (h[i] > h[i - 1] && h[i] > h[i - 2] && h[i] > h[i + 1] && h[i] > h[i + 2]) {
				int max = h[i - 2];
				if (h[i - 1] > max)
					max = h[i - 1];
				if (h[i + 1] > max)
					max = h[i + 1];
				if (h[i + 2] > max)
					max = h[i + 2];
				
				ans += h[i] - max;
			}
		}

		cout << "#" << test_case << " " << ans << '\n';
	}

	return 0;
}
profile
👩🏻‍💻

0개의 댓글