zoukankan      html  css  js  c++  java
  • 问题 A: [Usaco2006 Mar]Mooo 奶牛的歌声

    几个月前在学校zxr dalao讲过单调栈,所以对于单调栈还是有一些了解,这道题根据题意,声音只能被身高大于它的奶牛听见,也就是说,每一个奶牛只能听见身高比他矮的奶牛的声音。

    所以我们需要维护一个单调递减的单调栈,遇到一个比它高的奶牛就将其弹出栈,而那头高的奶牛听到的声音就是被弹出栈的奶牛声音的总和。

    #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;
    }
  • 相关阅读:
    为什么要用do-while(0)?
    网络字节序&大小端存储
    sql语句w3school教程
    C++编码规范
    std::deque双端队列介绍
    gdb基本操作
    gdb调试多线程
    数据库基础
    删除vector所有元素
    stl迭代器失效
  • 原文地址:https://www.cnblogs.com/LJB666/p/10994568.html
Copyright © 2011-2022 走看看