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

【题解】

  维护一个单调栈即可。 但是因为有相同身高的存在,所以要稍微考虑下相同身高的处理。因为这个卡了一下下QAQ...

  

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define LL long long
 5 #define rg register
 6 #define N 500010
 7 using namespace std;
 8 int n,top,s[N],cnt[N];
 9 LL ans;
10 inline int read(){
11     int k=0,f=1; char c=getchar();
12     while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
13     while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
14     return k*f;
15 }
16 int main(){
17     n=read();
18     while(n--){
19         int x=read();
20         while(s[top]<x&&top) ans+=cnt[top]+1,cnt[top--]=0;
21         if(x==s[top]){
22             ans+=++cnt[top];
23             if(top>1) ans++;
24         }
25         else{
26             if(top) ans++;
27             s[++top]=x;
28         }
29 //        printf("ans=%d[",ans); for(rg int i=1;i<=top;i++) printf("%d ",s[i]); puts("]");
30     }
31     return printf("%lld\n",ans),0;
32 }
View Code

 

转载于:https://www.cnblogs.com/DriverLao/p/9391821.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值