Little Jay

[C++] 백준 17608번 막대기 본문

알고리즘/BOJ

[C++] 백준 17608번 막대기

Jay, Lee 2021. 7. 30. 21:27

조금만 입체적으로 생각하면 되는 문제이다.

stack의 선입선출의 구조를 생각하면 되는 문제인데,

단순히 마지막에 있는 막대기보다 큰 막대기를 세는 것이 아니라

막대기의 앞뒤를 비교하면 쉽게 풀 수 있다.

 

#include <iostream>
#include <stack>
using namespace std;

int main() {

	int n;
	cin >> n;
	stack<int> st;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		st.push(x);
	}

	int temp = st.top();
	int count = 1;
	while (!st.empty()) {
		if (st.top() > temp) {
			temp = st.top();
			count++;
		}
		st.pop();
	}
	cout << count << "\n";

	return 0;
}
Comments