BZOJ 1657 奶牛的歌声

单调栈。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 50050
using namespace std;
int n,v[maxn],h[maxn],s[maxn],ans[maxn],t=0;
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d%d",&h[i],&v[i]);
    for (int i=1;i<=n;i++)
    {
        while (t && h[i]>h[s[t]]) ans[i]+=v[s[t--]];
        s[++t]=i;
    }
    t=0;
    for (int i=n;i>=1;i--)
    {
        while (t && h[i]>h[s[t]]) ans[i]+=v[s[t--]];
        s[++t]=i;
    }
    int mx=0;
    for (int i=1;i<=n;i++) mx=max(mx,ans[i]);
    printf("%d\n",mx);
    return 0;
}
时间: 2024-11-06 03:55:45

BZOJ 1657 奶牛的歌声的相关文章

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

从左往右,从右往左 扫两遍,单调栈维护... ---------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<stack> #include<iostream> #define rep( i , n ) for( int i = 0 ; i &

[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

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

[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

[bzoj 2097]奶牛健美操

题目描述 对于一棵n个点的树,删除k条边,使得所有联通块直径最大值最小 题解 首先二分联通块直径最大值的最小值. 那么这个能否达成的判定变成了一个类似树形dp的东西 对于一个子树,删除一条边可以删除整个子树 对于所有子树,从到达最优答案时的深度,最大的开始删除,如果当前最大值+次大值<=k时退出循环 那么有一个二分的log  加上每次转移的sort,复杂度就是 #include<map> #include<stack> #include<queue> #inclu

BZOJ 1706 奶牛接力跑

倍增floyd.有点卡内存,要随着一起得出那个f. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define maxv 220 #define maxe 110 using namespace std; int n,m,S,E,hash[maxv],tot=0,flag=0; int f[maxv][maxv]

BZOJ 1827 奶牛大集会

树型DP. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxv 200500 #define maxe 300500 #define inf 1000000007 using namespace std; long long n,x,y,z,c[maxv],nume=0,g[maxv],val[maxv],sum=0,ans,dis[ma

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

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