#include <stdio.h>
#include <stack>
using namespace std;
int N,D[300030],L[300030],R[300030];
stack<pair<int, int> > S; int lf;
int main()
{
scanf ("%d",&N);
for (int i=0;i<N;i++) scanf ("%d",&D[i]);
for (int i=0;i<N;i++){
while (!S.empty() && S.top().first < D[i]){
lf -= S.top().second;
S.pop();
}
L[i] = lf;
if (!S.empty() && S.top().first == D[i]){
L[i] -= S.top().second;
S.top().second++;
}
else S.push(make_pair(D[i],1));
lf++;
}
while (!S.empty()) S.pop(); lf = 0;
for (int i=N-1;i>=0;i--){
while (!S.empty() && S.top().first < D[i]){
lf -= S.top().second;
S.pop();
}
R[i] = lf;
if (!S.empty() && S.top().first == D[i]){
R[i] -= S.top().second;
S.top().second++;
}
else S.push(make_pair(D[i],1));
lf++;
}
long long ans = 0;
for (int i=0;i<N;i++) ans += L[i] < R[i] ? L[i] : R[i];
printf ("%lld\n",ans);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4756 KB |
Output is correct |
2 |
Correct |
0 ms |
4756 KB |
Output is correct |
3 |
Incorrect |
0 ms |
4756 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
4756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
4756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
4756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |