[Usaco2006 Mar]Mooo 奶牛的歌声(单调栈裸题)
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
4 2
3 5
6 10
INPUT DETAILS:
Three cows: the first one has height 4 and moos with volume 2, etc.
Sample Output
HINT
队伍中的第3头奶牛可以听到第1头和第2头奶牛的歌声,于是她能听到的总音量为2+5=7.虽然她唱歌时的音量为10,但并没有奶牛可以听见她的歌声.
Source
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 奶牛的歌声(单调栈裸题)的更多相关文章
- bzoj 1657 [Usaco2006 Mar]Mooo 奶牛的歌声——单调栈水题
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1657 #include<iostream> #include<cstdio ...
- Bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声 单调栈
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 631 Solved: 445[Submi ...
- [BZOJ1657] [Usaco2006 Mar] Mooo 奶牛的歌声 (单调栈)
Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mo ...
- BZOJ1657: [Usaco2006 Mar]Mooo 奶牛的歌声
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 489 Solved: 338[Submi ...
- 1657: [Usaco2006 Mar]Mooo 奶牛的歌声
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 526 Solved: 365[Submi ...
- bzoj 1657 Mooo 奶牛的歌声 —— 单调栈
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1657 单调栈水题. 代码如下: #include<iostream> #incl ...
- 【BZOJ】1657: [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1657 这一题一开始我想到了nlog^2n的做法...显然可做,但是麻烦.(就是二分+rmq) 然后我 ...
- BZOJ 1657 [Usaco2006 Mar]Mooo 奶牛的歌声:单调栈【高度序列】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1657 题意: Farmer John的N(1<=N<=50,000)头奶牛整齐 ...
- [Usaco2006 Mar]Mooo 奶牛的歌声
Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mo ...
随机推荐
- js带文字的圆随机运动
首先是html代码(其实就只有一个画布,记得要把外部js引入写在body底部 <!doctype html> <html> <head> <meta http ...
- html+css定位篇
position absolute相对于父元素移动,不在父元素范围内时,可能和其他元素重叠 relative相对于初始位置来进行移动 fixed相对于浏览器进行定位,无论滑轮如何滚动,始终出现在浏览器 ...
- Shader之ShaderUI使用方法
shader中的宏定义在material中Inspector中打开 Shader "Custom/Redify" { Properties{ _MainTex("Base ...
- Android studio 配置使用maven
安装nexus(略) 启动nexus 打开web(admin;admin123) http://127.0.0.1:8081/nexus 创建的demo 1 2 3 对应的本地目录 配置maven / ...
- 基于 Azure 托管磁盘配置高可用共享文件系统
背景介绍 在当下,共享这个概念融入到了人们的生活中,共享单车,共享宝马,共享床铺等等.其实在 IT 界,共享这个概念很早就出现了,通过 SMB 协议的 Windows 共享目录,NFS 协议的网络文件 ...
- PowerBuilder与嵌入浏览器交互
准备工作1. 新增一个Application应用,新增一个窗口.2. 在窗口中新增一个OLE控件:Microsoft Web Browser,命名为old_1.3. 新增一个TextBox网址输入控件 ...
- Selenium2学习(十五)-- 单选框和复选框(radiobox、checkbox)
本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...
- 【Spring实战】—— 1 入门讲解
这个系列是学习spring实战的总结,一方面总结书中所写的精髓,另一方面总结一下自己的感想. 基础部分讲解了spring最为熟知的几个功能:依赖注入/控制反转 和 面向切面编程. 这两个就不再多说了, ...
- 2.Zabbix 3.0 部署
请查看我的有道云笔记地址: http://note.youdao.com/noteshare?id=0db90549f9f347faf928b781087b28c9&sub=AAA6CE2FA ...
- OpenGL中的数据——Buffer
OpenGL中主要包括了两种数据——Buffer和Texture. Buffer用于储存线性数无类型据块,可以看成普通的内存块,而Texture则用于储存多维数据,一般储存图像或者其他数据. Buff ...