SWEA 1206 View
2019. 7. 25. 08:35ㆍ알고리즘/삼성
양 옆 2칸의 최대값을 구하고, 또 거기서의 최댓값을 구한 후 비교해서 더해가는 방식으로 해결할 수 있다
문제: https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV134DPqAA8CFAYh
깃허브주소: https://github.com/surinoel/boj/blob/master/swea1206.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstring> | |
#include <iostream> | |
using namespace std; | |
#define max(n, m) n > m ? n : m | |
#define min(n, m) n > m ? m : n | |
int mat[1000]; | |
int main(void) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
for (int tc = 1; tc <= 10; tc++) { | |
memset(mat, 0, sizeof(mat)); | |
int n; | |
cin >> n; | |
for (int j = 0; j < n; j++) { | |
cin >> mat[j]; | |
} | |
int ans = 0; | |
for (int j = 0; j < n; j++) { | |
int minleft = 0, minright = 0; | |
if (j - 2 >= 0) { | |
minleft = max(minleft, mat[j - 2]); | |
} | |
if (j - 1 >= 0) { | |
minleft = max(minleft, mat[j - 1]); | |
} | |
if (j + 1 < n) { | |
minright = max(minright, mat[j + 1]); | |
} | |
if (j + 2 < n) { | |
minright = max(minright, mat[j + 2]); | |
} | |
int maxheight = max(minleft, minright); | |
if (mat[j] > maxheight) { | |
ans += mat[j] - maxheight; | |
} | |
} | |
cout << "#" << tc << " " << ans << '\n'; | |
} | |
return 0; | |
} |
'알고리즘 > 삼성' 카테고리의 다른 글
2382 미생물 격리 (0) | 2019.08.05 |
---|---|
2383 점심 식사시간 (0) | 2019.08.03 |
2105 디저트 카페 (0) | 2019.08.03 |
SWEA 1208 Flatten (0) | 2019.07.25 |
코드그라운드 개구리뛰기 (0) | 2019.06.27 |