//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << " " <<
#define nl << "\n"
signed main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n;
int a[n], ans = 0;
for(int& i : a) cin >> i;
stack<int> s;
s.push(0);
for(int i=0; i<n; ++i){
while(!s.empty() and s.top() > a[i]){
s.pop();
++ans;
}
if(s.empty() or s.top() < a[i]) s.push(a[i]);
}
while(!s.empty()){
s.pop();
++ans;
}
cout << ans-1 nl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
4 ms |
748 KB |
Output is correct |
5 |
Correct |
6 ms |
876 KB |
Output is correct |
6 |
Correct |
12 ms |
1516 KB |
Output is correct |
7 |
Correct |
13 ms |
1260 KB |
Output is correct |