본문 바로가기

algorithm

백준 17608번 막대기

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

 

아이디어:

1. 우선 스택에 다 넣고, 마지막부터 비교하면서 뺀다.

 

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int N, tmp, ans = 0;
	stack<int> S;
	cin >> N;

	while (N--) {
		cin >> tmp;
		S.push(tmp);
	}

	tmp = S.top();

	while (!S.empty()) {
		if (S.top() > tmp) {
			tmp = S.top();
			ans += 1;
		}
		S.pop();

	}

	cout << ans + 1;
	return 0;

}