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

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

[Submit][Status][Discuss]

HOME Back

思路:

显然的一道单调栈裸题

我们开一个非严格单调递减栈

如果当前的的元素比栈顶元素小或相等

那么根据题意,声波就一定撞不上

如果大于,那么前面的声音就会撞在这个上面,我们就要出栈

出栈时记下来是因为哪个元素而出栈的即可

因为声波是双向的,所以我们要正着跑一遍,反着跑一遍

OK

顺手吐槽一下版子题为什么是权限题qwq

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rii register int i
#define rij register int j
using namespace std;
int l[],r[],h[],stack[],v[],ans[];
int n;
bool cmp(int lk,int kl)
{
return lk>kl;
}
int main()
{
scanf("%d",&n);
for(rii=;i<=n;i++)
{
scanf("%d%d",&h[i],&v[i]);
}
int cnt=;
for(rii=;i<=n;i++)
{
if(cnt==)
{
cnt++;
stack[cnt]=i;
continue;
}
if(h[stack[cnt]]>h[i])
{
cnt++;
stack[cnt]=i;
continue;
}
while()
{
if(h[stack[cnt]]<h[i])
{
r[stack[cnt]]=i;
cnt--;
}
else
{
cnt++;
stack[cnt]=i;
break;
}
if(cnt==)
{
cnt++;
stack[cnt]=i;
break;
}
}
}
while(cnt!=)
{
r[stack[cnt]]=n+;
cnt--;
}
for(rii=n;i>=;i--)
{
if(cnt==)
{
cnt++;
stack[cnt]=i;
continue;
}
if(h[stack[cnt]]>h[i])
{
cnt++;
stack[cnt]=i;
continue;
}
while()
{
if(h[stack[cnt]]<h[i])
{
l[stack[cnt]]=i;
cnt--;
}
else
{
cnt++;
stack[cnt]=i;
break;
}
if(cnt==)
{
cnt++;
stack[cnt]=i;
break;
}
}
}
while(cnt!=)
{
l[stack[cnt]]=;
cnt--;
}
for(rii=;i<=n;i++)
{
ans[l[i]]+=v[i];
ans[r[i]]+=v[i];
}
sort(ans+,ans+n+,cmp);
cout<<ans[];
}

[Usaco2006 Mar]Mooo 奶牛的歌声(单调栈裸题)的更多相关文章

  1. bzoj 1657 [Usaco2006 Mar]Mooo 奶牛的歌声——单调栈水题

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1657 #include<iostream> #include<cstdio ...

  2. Bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声 单调栈

    1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 631  Solved: 445[Submi ...

  3. [BZOJ1657] [Usaco2006 Mar] Mooo 奶牛的歌声 (单调栈)

    Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mo ...

  4. BZOJ1657: [Usaco2006 Mar]Mooo 奶牛的歌声

    1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 489  Solved: 338[Submi ...

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

    1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 526  Solved: 365[Submi ...

  6. bzoj 1657 Mooo 奶牛的歌声 —— 单调栈

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1657 单调栈水题. 代码如下: #include<iostream> #incl ...

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

    http://www.lydsy.com/JudgeOnline/problem.php?id=1657 这一题一开始我想到了nlog^2n的做法...显然可做,但是麻烦.(就是二分+rmq) 然后我 ...

  8. BZOJ 1657 [Usaco2006 Mar]Mooo 奶牛的歌声:单调栈【高度序列】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1657 题意: Farmer John的N(1<=N<=50,000)头奶牛整齐 ...

  9. [Usaco2006 Mar]Mooo 奶牛的歌声

    Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mo ...

随机推荐

  1. JavaScript数组常用操作总结

    我们在日常开发过程中,使用到原生 JavaScript的时候,有时候会频繁的对数组进行操作,今天我把工作以来,经常用到的有关 JavaScript数组的方法总结一下,方便日后工作的时候查找使用! 一. ...

  2. 2017年10月9日 冒泡&去重复习

    今天看了一下,就是数组跟js还是不太熟悉 冒泡排序    var arr = [4, 2, 1, 3, 6, 5];        for(var i = 1; i < arr.length; ...

  3. vs.net打包(自动检测环境并安装.net framwork)

    vs.net打包程序或者制作安装程序时自动检测环境并安装.netframwork的设置方法之前我看过文档也做过,但是过一段时间又忘了,现在终于又找到方法了,还是把这个方法写下来吧,方便自己也方便大家将 ...

  4. win10下MySQL 5.7.20解压版安装步骤

    1.从官网下载MySQL5.7.20解压版64位:https://dev.mysql.com/downloads/file/?id=473309. 2.解压(我的解压路径为:E:\mysql-5.7. ...

  5. 3、HTML属性

    属性的意义是为HTML提供附加信息. 属性中,名称和值总是成对出现.比如 <img src="1" width="2" /> src="1 ...

  6. JQuery和html+css实现鼠标点击放烟花

    <!DOCTYPE html> <html> <head><meta http-equiv="Content-Type" content= ...

  7. Java使用imageio、awt生成图片验证码

    1.生成验证码工具类 public class CheckCodeTool { private Integer width = 80; private Integer height = 38; pub ...

  8. 实现动态代理(Java和spring)

    一.Java实现动态代理 1.创建接口 package com.oyy.mw.biz.i; public interface Cal { public int add(int num1,int num ...

  9. ubuntu怎么关防火墙

    1.关闭ubuntu的防火墙 ufw disable2.卸载了iptables apt-get remove iptables 1.用iptables -F这个命令来关闭防火墙,但是使用这个命令前,千 ...

  10. 【面试虐菜】—— 常用SQL语句

    创建表 create table emp( id decimal, name ), age decimal) 删除表 DROP TABLE EMP 插入默认值 CREATE TABLE EMP( Id ...