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

猜你喜欢

转载自www.cnblogs.com/DriverLao/p/9391821.html