• [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈裸题)


    1657: [Usaco2006 Mar]Mooo 奶牛的歌声

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 961  Solved: 679
    [Submit][Status][Discuss]

    Description

    Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mooing. Each cow has a unique height h in the range 1..2,000,000,000 nanometers (FJ really is a stickler for precision). Each cow moos at some volume v in the range 1..10,000. This "moo" travels across the row of cows in both directions (except for the end cows, obviously). Curiously, it is heard only by the closest cow in each direction whose height is strictly larger than that of the mooing cow (so each moo will be heard by 0, 1 or 2 other cows, depending on not whether or taller cows exist to the mooing cow's right or left). The total moo volume heard by given cow is the sum of all the moo volumes v for all cows whose mooing reaches the cow. Since some (presumably taller) cows might be subjected to a very large moo volume, FJ wants to buy earmuffs for the cow whose hearing is most threatened. Please compute the loudest moo volume heard by any cow.

    Farmer John的N(1<=N<=50,000)头奶牛整齐地站成一列“嚎叫”。每头奶牛有一个确定的高度h(1<=h<=2000000000),叫的音量为v (1<=v<=10000)。每头奶牛的叫声向两端传播,但在每个方向都只会被身高严格大于它的最近的一头奶牛听到,所以每个叫声都只会 被0,1,2头奶牛听到(这取决于它的两边有没有比它高的奶牛)。 一头奶牛听到的总音量为它听到的所有音量之和。自从一些奶牛遭受巨大的音量之后,Farmer John打算买一个耳罩给被残害得最厉 害的奶牛,请你帮他计算最大的总音量。

    Input

    * Line 1: A single integer, N.

    * Lines 2..N+1: Line i+1 contains two space-separated integers, h and v, for the cow standing at location i.

        第1行:一个正整数N.

        第2到N+1行:每行包括2个用空格隔开的整数,分别代表站在队伍中第i个位置的奶牛的身高以及她唱歌时的音量.

    Output

    * Line 1: The loudest moo volume heard by any single cow.

        队伍中的奶牛所能听到的最高的总音量.

    Sample Input

    3
    4 2
    3 5
    6 10

    INPUT DETAILS:

    Three cows: the first one has height 4 and moos with volume 2, etc.

    Sample Output

    7

    HINT

         队伍中的第3头奶牛可以听到第1头和第2头奶牛的歌声,于是她能听到的总音量为2+5=7.虽然她唱歌时的音量为10,但并没有奶牛可以听见她的歌声.

    Source

    [Submit][Status][Discuss]


    HOME Back

    思路:

    显然的一道单调栈裸题

    我们开一个非严格单调递减栈

    如果当前的的元素比栈顶元素小或相等

    那么根据题意,声波就一定撞不上

    如果大于,那么前面的声音就会撞在这个上面,我们就要出栈

    出栈时记下来是因为哪个元素而出栈的即可

    因为声波是双向的,所以我们要正着跑一遍,反着跑一遍

    OK

    顺手吐槽一下版子题为什么是权限题qwq

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define rii register int i
    #define rij register int j
    using namespace std;
    int l[50005],r[50005],h[50005],stack[50005],v[50005],ans[50005];
    int n;
    bool cmp(int lk,int kl)
    {
        return lk>kl;
    }
    int main()
    {
        scanf("%d",&n);
        for(rii=1;i<=n;i++)
        {
            scanf("%d%d",&h[i],&v[i]);
        }
        int cnt=0;
        for(rii=1;i<=n;i++)
        {
            if(cnt==0)
            {
                cnt++;
                stack[cnt]=i;
                continue;
            }
            if(h[stack[cnt]]>h[i])
            {
                cnt++;
                stack[cnt]=i;
                continue;
            }
            while(1)
            {
                if(h[stack[cnt]]<h[i])
                {
                    r[stack[cnt]]=i;
                    cnt--;
                }
                else
                {
                    cnt++;
                    stack[cnt]=i;
                    break;
                }
                if(cnt==0)
                {
                    cnt++;
                    stack[cnt]=i;
                    break;
                }
            }
        }
        while(cnt!=0)
        {
            r[stack[cnt]]=n+1;
            cnt--;
        }
        for(rii=n;i>=1;i--)
        {
            if(cnt==0)
            {
                cnt++;
                stack[cnt]=i;
                continue;
            }
            if(h[stack[cnt]]>h[i])
            {
                cnt++;
                stack[cnt]=i;
                continue;
            }
            while(1)
            {
                if(h[stack[cnt]]<h[i])
                {
                    l[stack[cnt]]=i;
                    cnt--;
                }
                else
                {
                    cnt++;
                    stack[cnt]=i;
                    break;
                }
                if(cnt==0)
                {
                    cnt++;
                    stack[cnt]=i;
                    break;
                }
            }
        }
        while(cnt!=0)
        {
            l[stack[cnt]]=0;
            cnt--;
        }
        for(rii=1;i<=n;i++)
        {
            ans[l[i]]+=v[i];
            ans[r[i]]+=v[i];
        }
        sort(ans+1,ans+n+1,cmp);
        cout<<ans[1];
    }
  • 相关阅读:
    shell脚本通过ping命令来获取平均延时
    源码大招:不服来战!撸这些完整项目,你不牛逼都难!
    最新最全的 Android 开源项目合集
    3.环境搭建-Hadoop(CDH)集群搭建
    mycat安装和测试
    LVS+keepalived
    Linux上利用NFS实现远程挂载
    CentOS下MySQL主从同步配置
    Nginx配置文件(nginx.conf)配置详解
    Storm集群搭建
  • 原文地址:https://www.cnblogs.com/ztz11/p/9699602.html
Copyright © 2020-2023  润新知