zoukankan      html  css  js  c++  java
  • 洛谷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 } 
  • 相关阅读:
    Ajax 异步请求返回集合遍历问题
    JS 类数组,字符串,转换成数组的方法
    laravel、TP、YII三个框架的优缺点对比
    纵深防御
    渗透测试小结
    常见的设计模式
    CDN简介
    WAF小介
    分布式事务及其常见的解决方案
    redis主从复制
  • 原文地址:https://www.cnblogs.com/yu-xing/p/10162848.html
Copyright © 2011-2022 走看看