#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
int n;
int ar[500005];
signed main(){
int ans=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>ar[i];
}
stack<int>stk;
for(int i=1;i<=n;i++){
while(stk.size()&&stk.top()>ar[i]){stk.pop();}
if(stk.size()&&stk.top()==ar[i]){continue;}
if(ar[i]==0){continue;}
stk.push(ar[i]);
ans++;
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
712 KB |
Output is correct |
5 |
Correct |
10 ms |
1088 KB |
Output is correct |
6 |
Correct |
29 ms |
2464 KB |
Output is correct |
7 |
Correct |
29 ms |
1992 KB |
Output is correct |