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. Spring 的ioc

    Spring的两个非常重要的功能ioc和aop 依赖反转:依赖对象的获得被反转:很多非凡的应用都是由两个或多个类通过彼此的合作来上线业务逻辑.这使得每一个对象都须要与其它对象合作,也就是说 一个对象获 ...

  2. react 调用项目中的 .html 文件

    (1)将 html 文件 放于 public 文件夹下 (2)window.open('about:blank').location.href="http://localhost:3000/ ...

  3. 〖Linux〗不重复启动某应用程序的脚本

    cmd="/home/scue/bin/ipclient $ipclient" exist=$(ps aux | grep -v 'grep' | grep "$cmd& ...

  4. iOS 烟花撒花效果,图层渐变,图层倒影特效。CAEmitterLayer粒子发射器

    iOS 烟花撒花效果,图层渐变,图层倒影特效.CAEmitterLayer粒子发射器 上一节我写了一个关于视图图层的相关类,有关CALayer这个类的使用和一些使用方法,详细看这里,就是我们在处理视图 ...

  5. 转:configure/make/make install的作用 linux 安装 卸载 make uninstall

    这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤. ./configure 是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不是需要CC或GC ...

  6. QueryRunner 结果处理器

    package cn.itcast.dbutil; import java.sql.SQLException; import java.util.List; import java.util.Map; ...

  7. vmware-Binary translation is incompatible with long mode on this platform

    解决方法:Binary translation is incompatible with long mode on this platform. Disabling long mode. Withou ...

  8. Android设计模式系列(2)--SDK源码之观察者模式

    观察者模式,是一种非常常见的设计模式,在很多系统中随处可见,尤其是涉及到数据状态发生变化需要通知的情况下.本文以AbstractCursor为例子,展开分析.观察者模式,Observer Patter ...

  9. 关于windows10 CMD 的一些操作

    之前接触过cmd的一些操作方法,比如用dir.tasklist等一些方法,但是用了会立马忘记,再用到时又要重新google,这着实让我头痛!!! 今天又碰到一个关于改变目录的问题,又是纠结万分,所以就 ...

  10. mfc获取控件在对话框上的位置

    CRect rect; GetDlgItem(控件ID)->GetWindowRect(&rect);//获取控件的屏幕坐标ScreenToClient(&rect);//转换为 ...