Luogu P1901 launch station

Luogu P1901 launch station

Watching for a long time will only be violent. . .
Then turned over the problem solution
Monotonic stack? ? ? ! !what is this. . .
Many of the ideas of the solution are incomprehensible, and
some annotations are also incomprehensible
. . .
Finally, there is a very friendly but unannotated problem solution.
I simulated it against the example and got it! !
(It’s better to do it with hands-on practice)
It’s amazing, I feel like I’ve learned something new.
It so simple.

//左右各一次单调栈
#include<cstdio>
#include<cstring>

int a[1000010],h[1000010],z[1000010];
int sum[1000010];
int top=0,n,ans=0;

int work(int x)
{
    while(top>0&&h[z[top]]<h[x])
        top--;//比我矮当然不给它能量 
    sum[z[top]]+=a[x];//当前top就是比我高还离我最近的 
    top++;z[top]=x;//进栈 
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d %d",&h[i],&a[i]);
    for(int i=1;i<=n;i++)//左边 
        work(i);
    top=0;
    for(int i=n;i>=1;i--)//右边 
        work(i);
    for(int i=1;i<=n;i++)
        if(sum[i]>ans)ans=sum[i];
    printf("%d",ans);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726424&siteId=291194637