【BZOJ】1657: [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1657
这一题一开始我想到了nlog^2n的做法。。。显然可做,但是麻烦。(就是二分+rmq)
然后我仔细的想了想,恩,对,单调栈可以完成。。。他们有传递性的。。
然后你懂的。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=50005;
int s[N], top, h[N], n, L[N], R[N], power[N], sum[N], ans; void update(int x) {
bool flag=1;
while(top) {
if(h[x]>=h[s[top]]) {
if(h[x]==h[s[top]]) {
L[x]=s[top];
R[top]=x;
flag=0;
}
else R[s[top]]=x;
--top;
}
else break;
}
if(flag) L[x]=s[top];
s[++top]=x;
} int main() {
read(n);
for1(i, 1, n) { read(h[i]); read(power[i]); }
for1(i, 1, n) update(i);
for1(i, 1, n) {
sum[L[i]]+=power[i];
sum[R[i]]+=power[i];
}
for1(i, 1, n) ans=max(ans, sum[i]);
print(ans);
return 0;
}
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& lt;=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
【BZOJ】1657: [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈)的更多相关文章
- Bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声 单调栈
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 631 Solved: 445[Submi ...
- bzoj 1657 [Usaco2006 Mar]Mooo 奶牛的歌声——单调栈水题
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1657 #include<iostream> #include<cstdio ...
- BZOJ 1657 [Usaco2006 Mar]Mooo 奶牛的歌声:单调栈【高度序列】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1657 题意: Farmer John的N(1<=N<=50,000)头奶牛整齐 ...
- [BZOJ1657] [Usaco2006 Mar] Mooo 奶牛的歌声 (单调栈)
Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mo ...
- bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声【单调栈】
先考虑只能往一边传播,最后正反两边就行 一向右传播为例,一头牛能听到的嚎叫是他左边的牛中与高度严格小于他并且和他之间没有更高的牛,用单调递减的栈维护即可 #include<iostream> ...
- 1657: [Usaco2006 Mar]Mooo 奶牛的歌声
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 526 Solved: 365[Submi ...
- BZOJ1657: [Usaco2006 Mar]Mooo 奶牛的歌声
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 489 Solved: 338[Submi ...
- [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈裸题)
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 961 Solved: 679[Submi ...
- [Usaco2006 Mar]Mooo 奶牛的歌声
Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mo ...
随机推荐
- jQuery如何获得select选中的值?input单选radio选中的值
jQuery取得select选中的值 本来以为jQuery("#select1").val();是取得选中的值, 那么jQuery("#select1").te ...
- iOS中使用iCloud一些须要注意的地方(Xcode7.2)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;) 在自己的App中怎样使用iCloud有非常多文章能够查阅,这里 ...
- CompletableFuture 详解
转 http://www.jianshu.com/p/6f3ee90ab7d3 CompletableFuture类实现了CompletionStage和Future接口.Future是Java 5添 ...
- 通过CSS3,实现元素覆盖效果
在非常多站点中,我们都能够看到这种效果.当用户鼠标进入某一个元素后,下方就会有遮罩层上浮动画,如图: 今天我们就用hover伪类加上css3实现,没有使用不论什么JS <!DOCTYPE htm ...
- 【php写日志】php将日志写入文件
php 写内容到文件,把日志写到log文件 <?php header("Content-type: text/html; charset=utf-8"); /******** ...
- composer error when run composer update
本篇文章由:http://xinpure.com/composer-error-when-run-composer-update/ 错误 很多时候即使是常用的命令也会出现一些奇奇怪怪的错误, 难以预知 ...
- PHP-权限控制类(转)
<?php /** * 权限控制类 */ class include_purview { /** * 类的唯一实例 */ private static $instance; /** * 权限常量 ...
- 摘:"error LNK2019: 无法解析的外部符号 该符号在函数 中被引用" 错误原因
例如“error LNK2019: 无法解析的外部符号error LNK2001: 无法解析的外部符号“private: static struct _OVERLAPPED CUsbCom::g_Wr ...
- C# 添加,修改,删除Xml节点 摘录
C# 添加,修改,删除Xml节点 //添加xml节点 private void AddXml(string image, string title) { XmlDocume ...
- 百度地图 驾车/公交查询Demo(支持多起点多终点)
效果图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...