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

题目链接
bzoj1657

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

code:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int a[50010],ans[50010],d[50010],z[50010],top;//a为身高,ans为伤残值,d为声音,z为栈,top为栈的大小
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&a[i],&d[i]);
    for(int i=1;i<=n;i++)
    {
        while(top&&a[i]>a[z[top]])//如果栈不为空且当前牛身高大于栈顶身高,就把栈顶牛踢掉,并把伤残值加上去
        {
            ans[i]+=d[z[top]];
            top--;
        }
        z[++top]=i;//加入栈
    }
    top=0;//清空栈
    for(int i=n;i>=1;i--)//逆序再做一遍
    {
        while(top&&a[z[top]]<a[i])
        {
            ans[i]+=d[z[top]];
            top--;
        }
        z[++top]=i;
    }
    int maxx=0;
    for(int i=1;i<=n;i++)//计算最大值
        maxx=max(maxx,ans[i]);
    printf("%d\n",maxx);
    return 0;
}

原文地址:https://www.cnblogs.com/hht2005/p/11378160.html

时间: 2024-11-06 07:36:57

[Usaco2006 Mar]Mooo 奶牛的歌声题解的相关文章

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

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

[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

矩阵乘法专题2——bzoj 1706 [usaco2007 Nov] relays 奶牛接力跑 题解

转载请注明:http://blog.csdn.net/jiangshibiao/article/details/24960651 [原题] 1706: [usaco2007 Nov]relays 奶牛接力跑 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 340  Solved: 162 [Submit][Status] Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项目.至于进行接力

【树形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 奶牛大集会

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