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

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

【BZOJ】1657: [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈)的更多相关文章

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

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

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

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

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

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

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

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

  5. bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声【单调栈】

    先考虑只能往一边传播,最后正反两边就行 一向右传播为例,一头牛能听到的嚎叫是他左边的牛中与高度严格小于他并且和他之间没有更高的牛,用单调递减的栈维护即可 #include<iostream> ...

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

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

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

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

  8. [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈裸题)

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

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

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

随机推荐

  1. 深入理解Git (三) - 微命令上篇

    1 git hash-object 曾经讲过Git用Hash值作为Git对象的名字,那么详细是哪个命令呢? 我们能够先改动一个文件: echo "hongchangfirst" & ...

  2. qq 自制表情包

      如何自制qq表情包? CreateTime--2018年2月5日15:34:35 Author:Marydon 1.添加表情 2.选择表情(可以进行多选) 3.对添加的表情进行分组 4.添加成功( ...

  3. 数据库选型之MySQL(普通硬盘)

    刘勇   Email:lyssym@sina.com 本博客记录作者在工作与研究中所经历的点滴,一方面给自己的工作与生活留下印记,另一方面若是能对大家有所帮助,则幸甚至哉矣! 简介 鉴于高频中心库ta ...

  4. Linux命令-压缩解压命令:bzip2、bunzip2

    bzip2是gzip的升级版 bzip2 [选项] 源文件名(压缩前) -k 保留源文件,(区别gzip不支持保留源文件) bunzip2 [选项] 源文件名(压缩后) 压缩文件: bzip2 -k ...

  5. jasmine-行为驱动测试

    http://jasmine.github.io/1.3/introduction.html,先保留着,好好研究.

  6. jquery 鼠标拖动排序Li或Table

    1.前端页面 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="拖动排序Li或Ta ...

  7. freeswitch 把SIP注册信息数据库从SQLITE 改为MYSQL的方法

    实际线上应用中,在线注册人数超过4000 ,SQLITE就吃不消了,容易造成锁表,考虑转入MYSQL,查了下官网 超过转入了MYSQL. https://wiki.freeswitch.org/wik ...

  8. atitit.提升兼容性最佳实践 o9o

    atitit.提升兼容性最佳实践 o9o.doc 1. Atitit.兼容性的"一加三"策略 1 2. 扩展表模式 1 3. 同时运行模式 1 3.1. 完美的后向兼容性 2 3. ...

  9. 云中的机器学习:FPGA 上的深度神经网络

    人工智能正在经历一场变革,这要得益于机器学习的快速进步.在机器学习领域,人们正对一类名为“深度学习”算法产生浓厚的兴趣,因为这类算法具有出色的大数据集性能.在深度学习中,机器可以在监督或不受监督的方式 ...

  10. struts2拦截器的实现原理及源码剖析

    拦截器(interceptor)是Struts2最强大的特性之一,也可以说是struts2的核心,拦截器可以让你在Action和result被执行之前或之后进行一些处理.同时,拦截器也可以让你将通用的 ...