思路:
转化成对于某一位置为最小值求向两边最远>=他的位置,用单调栈就能轻易完成。
那么ans=(left+right)*h[i];
维护单调递增还是递减呢?
我们能很快反应到,一旦碰到一个比他小的元素,那么之前的那个比他大的就要结束了。
ok,大致了解到碰到比他小的元素,那么比他大的呢?
也简单呀,对于比他大的元素,左端点已经找到了呀!
那么基于双方考虑,是不是就是维护单调递增栈呢?
如果碰到比top值大的,那么就压栈,并且左端点为i-1;
如果遇到比top值小的,要把栈里面值比他大的全部输出,因为已经找到右端点 i 了。
这么理解记忆还是略累啊。。。我就是如果我要以他为最小值两边延伸,那么就维护增,记忆成相反的。
而且比较喜欢用结构体表示一个结点所有信息,仅供参考吧~

//#include <bits/stdc++.h>
#include <iostream>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=1e5+10;
struct asd
{
LL left,right;
LL w;
};
LL h[N];
LL n;
stack<asd>q;
int main()
{
while(~scanf("%I64d",&n)&&n)
{
asd now,nex;
while(!q.empty())
q.pop();
for(int i=1; i<=n; i++)
scanf("%I64d",&h[i]);
LL ans=h[1];
now.left=0;
now.right=1;
now.w=h[1];
q.push(now);
for(int i=2; i<=n; i++)
{
now.left=0;
now.right=1;
now.w=h[i];
while(!q.empty()&&q.top().w>h[i])
{
nex=q.top();
q.pop();
now.left+=(nex.left+1);
LL temp=(nex.left+nex.right)*nex.w;
ans=max(ans,temp);
if(!q.empty())
q.top().right+=nex.right;
}
q.push(now);
}
while(!q.empty())
{
nex=q.top();
q.pop();
LL temp=(nex.left+nex.right)*nex.w;
ans=max(ans,temp);
if(!q.empty())
q.top().right+=nex.right;
}
printf("%I64d\n",ans);
}
return 0;
}

最新文章

  1. webform(九)——JQuery基础(选择器、事件、DOM操作)
  2. Spring Mock
  3. 关于nginx反向代理后获取不到客户端的真实ip地址问题
  4. 使用xpath时出现noDefClass的错误(找不到某个类)
  5. 蒙特卡洛树搜索算法(UCT): 一个程序猿进化的故事
  6. 博客已搬家至 hate13.com
  7. 257. Binary Tree Paths
  8. eclipse 的小技巧
  9. 关于错位动画的练习,原生js编写
  10. 检索表中所有列的名称、DB中的用户表
  11. c#中 HttpContext作用(一)【转】
  12. Mysql数据库多表查询
  13. bootstrap 的媒体查询
  14. 合并dict、list的方法
  15. 2. 搭建DRF项目
  16. ICP点云配准原理及优化
  17. 8.4Solr API使用(Result Grouping分组查询)
  18. 20155216 Exp4 恶意代码分析
  19. MySQL错误代码
  20. python学习之参数传递

热门文章

  1. ALV TREE 实例
  2. cron表达式(转)
  3. servlet 复习笔记
  4. 【zabbix】微信告警消息模版
  5. Git如何强制拉取一个远程分支到本地分支(转载)
  6. JAVA使用相对路径读取配置文件
  7. Express的基本使用
  8. C# HttpRequest
  9. matlab打开文件对话框
  10. poj-2336 Ferry Loading II(dp)