[Usaco2006 Mar]Mooo 奶牛的歌聲

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

Sample Output

7

HINT

 隊伍中的第3頭奶牛可以聽到第1頭和第2頭奶牛的歌聲,於是她能聽到的總音量爲2+5=7.雖然她唱歌時的音量爲10,但並沒有奶牛可以聽見她的歌聲.

單調棧,正着一遍反着一遍,維護嚴格遞減即可

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
    int x=0,f=1;char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())  if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())    x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x>=10)     print(x/10);
    putchar(x%10+'0');
}
const int N=5e4;
struct AC{
    int high,val;
    void join(){high=read(),val=read();}
}cow[N+10];
int stack[N+10],val[N+10];
int main(){
    int n=read(),ans=0;
    for (int i=1;i<=n;i++)  cow[i].join();
    for (int i=1,top=0;i<=n;i++){
        while (top&&cow[i].high>cow[stack[top]].high)   val[i]+=cow[stack[top--]].val;
        stack[++top]=i;
    }
    for (int i=n,top=0;i;i--){
        while (top&&cow[i].high>cow[stack[top]].high)   val[i]+=cow[stack[top--]].val;
        stack[++top]=i;
    }
    for (int i=1;i<=n;i++)  ans=max(ans,val[i]);
    printf("%d\n",ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章