※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 14008번 문제인 Medium Weird Measurements이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/14008 

 

14008번: Medium Weird Measurements

The intervals with measurements [1], [1,4], [4], [4], [4,2], [4,2,5], [4,2,5,2], [2], [2,5], [2,5,2], [5], [5,2], [2], [2,1], and [1] are weird. Note that there are TWO intervals that corresponds to the measurement [4], so it is counted twice.

www.acmicpc.net

14009번 문제에서 N의 제한이 작아진 문제이다. 풀이는 같으므로 해당 글을 참고하자.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;
typedef long long ll;

int arr[5001];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	ll ans = 0;
	int N; cin >> N;
	for (int i = 0; i < N; i++) cin >> arr[i];

	int cnt = 0;
	for (int i = 0; i < N; i++) {
		if (cnt == 0) cnt++;
		else if (cnt == 1) {
			if (arr[i - 1] != arr[i]) cnt++;
		}
		else {
			if (arr[i - 1] == arr[i]) cnt = 1;
			else if ((arr[i - 2] < arr[i - 1]) != (arr[i - 1] < arr[i])) cnt++;
			else cnt = 2;
		}

		ans += cnt;
	}

	cout << ans;
}
728x90

+ Recent posts