洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<stack>
 4 #include<cctype>
 5 using namespace std;
 6 stack<int> q;
 7 typedef long long LL;//不开long long见祖宗!!! 
 8 inline void read(LL &tmp)
 9 {
10     int x=1;char c=getchar();
11     for(tmp=0;!isdigit(c);c=getchar()) if(c=='-') x=-1;
12     for(;isdigit(c);tmp=tmp*10+c-48,c=getchar());
13     tmp*=x;
14 }
15 LL ans,tmp,n;
16 int main()
17 {
18     read(n);
19     while(n--)
20     {
21         read(tmp);
22         while(!q.empty()&&tmp>=q.top()) q.pop();//找矮个  注意等号!!! 
23         ans+=q.size();//当前剩下的牛都可以看见这头牛
24         q.push(tmp); //把这头牛加入到可以看见的序列
25     }
26     printf("%lld",ans);
27     return 0;
28 } 

 

posted @ 2018-12-22 20:14  宇興  阅读(217)  评论(0编辑  收藏  举报