Question A: [Usaco2006 Mar] Mooo cow song

 

A few months ago talked about in school zxr dalao monotonous stack, so for monotonous stack still has some understanding of this question meaning of the questions, the sound can only be heard height greater than its cows, that is to say, each cow can only hear shorter than his height sound cows.

So we need to maintain a stack monotonous monotone decreasing encountered a cow than it will pop up its stack, and the other end of the high cow sound is the sound of the cow is the sum of pop stack.

 

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;
int n;
int h[maxn];
int v[maxn];
int sta[maxn];
int ans[maxn];
int top;
int maxx;
void dandiaozhan()
{
    for(int i=1;i<=n;i++)
    {
        while(top&&h[i]>h[sta[top]])
        {
            ans[i]+=v[sta[top]];
            top--;
        }
        sta[++top]=i;
    }
    top=0;
    for(int i=n;i>=1;i--)
    {
        while(top&&h[i]>h[sta[top]])
        {
            ans[i]+=v[sta[top]];
            top--;
        }
        sta[++top]=i;
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&h[i],&v[i]);
    }
    dandiaozhan();
    for(int i=1;i<=n;i++)
    {
        maxx=max(ans[i],maxx);
    }
    printf("%d",maxx);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/LJB666/p/10994568.html