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;
}
}
'PS' 카테고리의 다른 글
[코드업] c++ 100제 (1001 - 1010) (0) | 2024.01.31 |
---|---|
SWEA [D3] - 18662. 등차수열 만들기 (C언어) (0) | 2023.11.08 |
SWEA [D1] - 2071. 평균값 구하기 (C언어) (0) | 2023.11.03 |
BOJ - 2217. 로프 (파이썬) (0) | 2023.09.18 |
BOJ - 5585. 거스름돈 (파이썬) (0) | 2023.09.08 |