单调栈版子

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int N=80010;
 5 int n,a,top=0,zh[N];
 6 int read(){
 7     int sum=0;
 8     char ch=getchar();
 9     while (ch<'0'||ch>'9')
10         ch=getchar();
11     while (ch>='0'&&ch<='9'){
12         sum=sum*10+ch-'0';
13         ch=getchar();
14     } 
15     return sum;
16 }
17 int main(){
18     long long ans=0;
19     scanf("%d",&n);
20     for (int i=1;i<=n;i++){
21         a=read();
22         while (a>=zh[top]&&top) top--;
23         ans+=top;
24         zh[++top]=a;
25     }
26     printf("%lld",ans);
27     return 0;
28 }
View Code

 

posted on 2016-11-15 14:15  Absolutezero  阅读(279)  评论(0编辑  收藏  举报