본문 바로가기
전공공부/코딩테스트

(c++) SWEA "1206) 1일차 - View"

by 시아나 2022. 5. 25.

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

 

SW Expert Academy

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

swexpertacademy.com


#include <iostream>
#include <vector>

using namespace std;

int solution(vector<int> &building, int N) {
	int sum = 0;
	for (int i = 2; i < N - 2; i++) {
		int max1 = max(building[i - 2], building[i - 1]);
		int max2 = max(building[i + 1], building[i + 2]);
		int total = max(max1, max2);
		sum += (building[i] > total) ? building[i] - total : 0;
	}
	return sum;
}

int main() {
	for (int t = 1; t <= 10; t++) {
		int N; cin >> N;
		vector<int> building;
		for (int i = 0; i < N; i++) {
			int n; cin >> n;
			building.push_back(n);
		}
		cout << "#" << t<<" "<< solution(building, N) << endl;
	}
	return 0;
}