프로그래밍 공부
BOJ/백준 3015 오아시스 재결합 본문
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
#define ll long long
#define height first
#define count second
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
stack<pair<int, int>> s;
ll ans = 0;
int n;
cin >> n;
vector<int> h(n);
for(int i = 0; i < n; i++){
cin >> h[i];
}
for(int i = 0; i < n; i++){
int tmp_h = h[i];
int cnt = 1;
while(!s.empty() && s.top().height <= h[i]){
ans += s.top().count;
if (s.top().height == tmp_h){
cnt += s.top().count;
}
s.pop();
}
if(!s.empty()){
ans += 1;
}
s.push({tmp_h, cnt});
}
cout << ans << '\n';
return 0;
}
'Problem Solving > Baekjoon Online Judge' 카테고리의 다른 글
BOJ/백준 17952 과제는 끝나지 않아! (0) | 2024.08.18 |
---|---|
BOJ/백준 25497 기술 연계마스터 임스 (0) | 2024.08.10 |
BOJ/백준 22352 항체 인식 (0) | 2021.08.02 |
BOJ/백준 7568 덩치 (0) | 2021.07.21 |
BOJ/백준 13459 숨바꼭질 3 (0) | 2021.07.05 |
Comments