BZOJ 1657: [Usaco2006 Mar]Mooo 奶牛的歌声( 单调栈 )

从左往右,从右往左 扫两遍,单调栈维护...

----------------------------------------------------------------------

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<stack>

#include<iostream>

#define rep( i , n ) for( int i = 0 ; i < n ; ++i )

#define clr( x , c ) memset( x ,c , sizeof( x ) )

using namespace std;

const int maxn = 50000 + 5;

struct COW {

int h , v;

void Read() {

scanf( "%d%d" , &h , &v );

}

};

COW cow[ maxn ];

int n;

int cnt[ maxn ];

stack< COW > S;

int main() {

freopen( "test.in" , "r" , stdin );

clr( cnt , 0 );

cin >> n;

rep( i , n ) cow[ i ].Read();

rep( i , n ) {

COW &o = cow[ i ];

while( ! S.empty() && o.h > S.top().h ) {

cnt[ i ] += S.top().v;

S.pop();

}

S.push( o );

}

while( ! S.empty() ) S.pop();

for( int i = n - 1 ; i >= 0 ; i-- ) {

COW &o = cow[ i ];

while( ! S.empty() && o.h > S.top().h ) {

cnt[ i ] += S.top().v;

S.pop();

}

S.push( o );

}

int ans = 0;

rep( i , n )

ans = max( ans , cnt[ i ] );

cout << ans << "\n";

return 0;

}

----------------------------------------------------------------------

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

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

时间: 2024-08-08 09:39:18

BZOJ 1657: [Usaco2006 Mar]Mooo 奶牛的歌声( 单调栈 )的相关文章

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

1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 858  Solved: 603[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

[BZOJ1657] [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 th

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

1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 526  Solved: 365[Submit][Status] 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

[BZOJ1657][Usaco2006 Mar]Mooo 奶牛的歌声

1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 863  Solved: 607 [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 heigh

[Usaco2006 Mar]Mooo 奶牛的歌声题解

题目链接 bzoj1657 思路: 本题可用单调栈思想来解.维护一个单调下降的序列,由于是下降的,所以栈里的每一头牛的声音都不能被后面的牛听到.当一个身高较大的牛加入栈时,栈不再单调,需要踢掉一些牛,而那些牛的身高都比它矮所以可以把那些牛的声音加到当前牛的伤残值里.而由于那些较矮的牛的声音会被阻挡,所以踢掉不会影响后续答案. 由于声音是双向传播的,所以需要正序做一遍,再逆序做一遍. code: #include<cstdio> #include<iostream> #include

【树形DP/搜索】BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会

1827: [Usaco2010 Mar]gather 奶牛大集会 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 793  Solved: 354[Submit][Status][Discuss] Description Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 N(1<=N<=100,000) 个农场中的一个,这些农场由N-1条道路连接,并且从任意一个

BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会( dp + dfs )

选取任意一个点为root , size[ x ] 表示以 x 为根的子树的奶牛数 , dp一次计算出size[ ] && 选 root 为集会地点的不方便程度 . 考虑集会地点由 x 点向它的子节点 son 转移 , 那么以 son 为集会地点比以 x 为集会地点要多 dist( x , son ) * ( tot - size[ x ] ) - dist( x , son ) * size[ x ] = dist( x , son ) * ( tot - 2 * size[ x ] )

BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会

Description Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 N(1<=N<=100,000) 个农场中的一个,这些农场由N-1条道路连接,并且从任意一个农场都能够到达另外一个农场.道路i连接农场A_i和B_i(1 <= A_i <=N; 1 <= B_i <= N),长度为L_i(1 <= L_i <= 1,000).集会可以在N个农场中的任意一个举行.另外

bzoj 1658: [Usaco2006 Mar]Water Slides 滑水

题解: 很神奇的做法,把点分成入度大于出度和入度小于出度两种. 然后入度大于出度的点必须走到某个点,所以排序贪心. #include<stdio.h> #include<iostream> #include<algorithm> #include<stdlib.h> using namespace std; const int N=10005; int n,m,i,ans,x,y,k,s,a[N],p[N],b[N],c[N]; inline void re