洛谷P1823 [COI2007] Patrik 音乐会的等待

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<stack>
 4 #include<cctype>
 5 #include<utility>
 6 using namespace std;
 7 typedef long long LL;
 8 typedef pair<LL,LL> p;//存储相同身高的人的数目  防止出现多个相同身高的人 
 9 inline void read(LL &tmp)
10 {
11     int x=1;char c=getchar();
12     for(tmp=0;!isdigit(c);c=getchar()) if(c=='-') x=-1;
13     for(;isdigit(c);tmp=tmp*10+c-48,c=getchar());
14     tmp*=x;
15 }
16 stack<p> q;
17 LL n,tmp,ans;
18 int main()
19 {
20     read(n);
21     for(int i=1;i<=n;i++)
22     {
23         read(tmp);
24         p now(tmp,1);//每种身高默认出现次数为1 
25         if(!q.empty()&&tmp<q.top().first) ++ans,q.push(now); //若新来的人比栈顶低 则只有其左边一人可看到他 
26         else
27         {
28             while(!q.empty()&&tmp>=q.top().first)  //若新来的人高于或等于栈顶 
29             {
30                 if(tmp==q.top().first) now.second+=q.top().second;//统计该身高出现次数 
31                 ans+=q.top().second;//单调栈内该种身高者都可以看到他
32                 q.pop();//弹出栈顶 
33             }
34             if(!q.empty()) ans++;//若栈不为空 说明其左边还存在一个比他高的人可以看到他 
35             q.push(now); 
36         }
37     }
38     printf("%lld",ans);
39     return 0;
40 } 

 

posted @ 2018-12-22 23:09  宇興  阅读(193)  评论(0编辑  收藏  举报