Dolphins의 HelloWorld

SW Expert Academy 4796. 의석이의 우뚝 선 산 D4 본문

Algorithm/SW Expert Academy

SW Expert Academy 4796. 의석이의 우뚝 선 산 D4

돌핀's 2018. 8. 21. 22:25

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWS2h6AKBCoDFAVT&categoryId=AWS2h6AKBCoDFAVT&categoryType=CODE


우뚝 선 산이 될 수 있는 case를 보면 예를들어 


h1 < h2 < h3 > h4 > h5 라고 할 때


h3 앞에 올 수 있는 경우의 수는 ( h1 ) , ( h1 h2 ) => 2가지 이고


h3 뒤에 올 수 있는 경우의 수는 ( h4 ) , ( h4 h5 ) = > 2가지 이다.


고로 우뚝 선 산이 될 수 있는 구간이 될 경우의 수는 2 * 2 = 4 가지 이다.


이런식으로 우뚝 선 산 전후로 오는 경우의 수가 몇가지인지 체크하고


곱한 것을 계속 더해나간다면 답을 구할 수 있다.


#include <iostream>

using namespace std;

long long arr[50001];
int main()
{
	int testcase;
	scanf("%d", &testcase);

	for (int i = 1; i <= testcase; i++) {
		int N;
		scanf("%d", &N);

		for (int j = 1; j <= N; j++) scanf("%lld", &arr[j]);
		int count1 = 0; //꼭대기 앞에 있는 산의 갯수
		int count2 = 0; //꼭대기 뒤에 있는 산의 갯수
		int result = 0;

		bool b = true;

		for (int k = 1; k <= N-1; k++) {
			if (b && arr[k + 1] > arr[k]) {
				count1++;
			}
			else if (!b && arr[k + 1] > arr[k]) {
				b = true;
				result += (count1 * count2);
				count1 = 1; count2 = 0;
				continue;
			}
			else if (b && arr[k + 1] < arr[k]) {
				b = false;
				count2++;
			}
			else if (!b && arr[k + 1] < arr[k]) {
				count2++;
			}
		}
		result += count1*count2;
		printf("#%d %d\n", i, result);
	}
}


Comments