stack No.2山峰问题

哈哈  想起是暑假在美国第一个题目  值得纪念!!

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<stack>
 5 using namespace std;
 6 int N,h[15010],n;//n是栈里的元素数量   N是一共有多少山
 7 stack<int> s;//建立一个栈
 8 int ans;
 9 
10 int main()
11 {
12 scanf("%d",&N);
13 for(int i=1;i<=N;i++)
14 {
15 scanf("%d",&h[i]);
16 ans+=n;//到达下一个山峰  加上之前栈里的元素   
17 while(n&&h[i]>s.top())//当当前山高度高于前一座山高度 出栈
18    {
19        s.pop();
20        n--;
21    }
22 s.push(h[i]);//把当前高度入栈
23 n++;
24 }
25 printf("%d",ans);
26 return 0;
27 } 

 

posted on 2016-03-16 00:40  babyyang  阅读(238)  评论(0编辑  收藏  举报