PS
BOJ 31858 : 간단한 순열 문제
lickelon
2024. 5. 20. 23:36
- 문제 링크 : boj.kr/31858
- 난이도 : G4
- 태그 : 애드혹, 스택
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 987654321
#define int ll
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
istream& operator>>(istream & ist, pii &p) {
ist >> p.first >> p.second;
return ist;
}
template <typename T>
istream& operator>>(istream & ist, vector<T> &arr) {
for(auto &u : arr) ist >> u;
return ist;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector<int> arr(n);
cin >> arr;
int ans = 0;
stack<int> _st;
for(auto u : arr) {
while(!_st.empty() && _st.top() < u) {
_st.pop();
ans++;
}
if(!_st.empty()) ans++;
_st.push(u);
}
cout << ans;
return 0;
}
풀이
#2493과 기본적인 논리는 유사하다. 스택을 유사한 방식으로 다루고, 스택에서 제거할 때 ans++, 스택에 자기보다 큰 수가 있을 때 ans++하면 된다.
728x90