zoukankan      html  css  js  c++  java
  • Bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声 单调栈

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

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 631  Solved: 445
    [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

    Silver

    题解:

    单调栈。

    正反各做一遍即可。。。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define MAXN 50010
     4 int h[MAXN],v[MAXN],H[MAXN],V[MAXN],a[MAXN];
     5 int read()
     6 {
     7     int s=0,fh=1;char ch=getchar();
     8     while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
     9     while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    10     return s*fh;
    11 }
    12 int main()
    13 {
    14     int n,i,top,MX;
    15     n=read();
    16     for(i=1;i<=n;i++)h[i]=read(),v[i]=read();
    17     top=0;
    18     memset(a,0,sizeof(a));
    19     for(i=1;i<=n;i++)
    20     {
    21         if(h[i]<=H[top])H[++top]=h[i],V[top]=v[i];
    22         else
    23         {
    24             while(top>0&&h[i]>H[top]){a[i]+=V[top--];}
    25             H[++top]=h[i];V[top]=v[i];
    26         }
    27     }
    28     top=0;
    29     memset(H,0,sizeof(H));
    30     memset(V,0,sizeof(V));
    31     for(i=n;i>=1;i--)
    32     {
    33         if(h[i]<=H[top])H[++top]=h[i],V[top]=v[i];
    34         else
    35         {
    36             while(top>0&&h[i]>H[top]){a[i]+=V[top--];}
    37             H[++top]=h[i];V[top]=v[i];
    38         }
    39     }
    40     MX=-1;
    41     for(i=1;i<=n;i++)MX=max(MX,a[i]);
    42     printf("%d",MX);
    43     return 0;
    44 }
  • 相关阅读:
    跟我学习编写通用的单据编码生成器
    asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析
    c#语言中的Process进程类型的使用示例
    C#语言中的XmlSerializer类的XmlSerializer.Deserialize (Stream)方法举例详解
    C#语言中的XmlSerializer类的XmlSerializer.Serialize(Stream,Object)方法举例详解
    大型三甲医院管理系统源码PACS超声科室源码DICOM影像工作站
    大型EMR电子病历源码三甲医院医疗信息管理系统软件网络版
    大型三甲医院信息管理系统源码 His系统功能齐全 完整可用
    大型三甲医院医疗体检信息管理系统源码 PEIS 体检科软件 CS
    大型进销存管理系统源码 家电业 电器类进销存 asp.net C#框架
  • 原文地址:https://www.cnblogs.com/Var123/p/5326894.html
Copyright © 2011-2022 走看看