알고리즘/백준
[백준/BOJ/3015] 오아시스 재결합
코딩하는 회사원
2024. 5. 24. 21:14
728x90
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int N;
vector<int> v;
stack<pii> s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> N;
v.resize(N);
for (int i = 0; i < N; i++) cin >> v[i];
ll res = 0;
for (int h : v) {
int cnt = 1;
while (!s.empty() && s.top().first <= h) {
res += s.top().second;
if (s.top().first == h) cnt += s.top().second;
s.pop();
}
if (!s.empty()) res += 1;
s.push({ h, cnt });
}
cout << res << "\n";
return 0;
}
728x90