PS
SWEA [D3] - 1206. [S/W 문제해결 기본] 1일차 - View (C언어)
kugnuoy
2023. 11. 4. 16:11
1206.
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV134DPqAA8CFAYh
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
[코드]
#include <stdio.h>
int max3(int a, int b, int c) {
if (a > b && a > c) return a;
else if (b > a && b > c) return b;
else return c;
}
int max4(int a, int b, int c, int d) {
if (a >= b && a >= c && a >= d) return a;
else if (b >= a && b >= c && b >= d) return b;
else if (c >= a && c >= b && c >= d) return c;
else return d;
}
int res = 0;
int main(){
for (int t = 0; t < 10; t++) {
int b[1000] = { 0, };
int N;
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &b[i]);
}
// first
if (b[0] > b[1] && b[0] > b[2])
{
if (b[1] < b[2]) res += b[0] - b[2];
else res += b[0] - b[1];
}
// second
if (b[1] > b[0] && b[1] > b[2] && b[1] > b[3])
{
res += b[1] - max3(b[0], b[2], b[3]);
}
// last
if (b[N - 1] > b[N - 2] && b[N - 1] > b[N - 3])
{
if (b[N - 2] < b[N - 3]) res += b[N - 1] - b[N - 3];
else res += b[N - 1] - b[N - 2];
}
// before last
if (b[N - 2] > b[N - 1] && b[N - 2] > b[N - 3] && b[N - 2] > b[N - 4])
{
res += b[N - 2] - max3(b[N - 1], b[N - 3], b[N - 4]);
}
for (int i = 2; i < N - 2; i++) {
if (b[i] > b[i - 2] && b[i] > b[i - 1] && b[i] > b[i + 1] && b[i] > b[i + 2]) {
res += b[i] - max4(b[i - 2], b[i - 1], b[i + 1], b[i + 2]);
}
}
printf("#%d %d\n", t+1, res);
res = 0;
}
}