洛谷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 } 

 

转载于:https://www.cnblogs.com/yu-xing/p/10162848.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值