Algorithm
[BOJ] 6198번 : 옥상 정원 꾸미기
rudgh99_algo
2024. 8. 7. 23:18
1. problem :
https://www.acmicpc.net/problem/6198
2. solution 1 :
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
stack<int> s;
long long ans = 0;
while (N--) {
int height;
cin >> height;
while (!(s.empty()) && s.top() <= height) {
s.pop();
}
ans += s.size();
s.push(height);
}
cout << ans << '\n';
}
ans를 while문안에서 더해주다가 틀렸다. 이때가 성립할 때는 감소하다가 증가하는 포인트가 있어야 성립한다. 따라서, while문 밖으로 ans를 빼주어야 한다.