POJ 2796:Feel Good(单调栈)
http://poj.org/problem?id=2796
题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少。
思路:只想到了O(n^2)的做法。
参考了http://www.cnblogs.com/ziyi--caolu/archive/2013/06/23/3151556.html的写法,用单调栈可以优化到O(n)。
对于每个元素,维护这个元素向前延伸比它大的有多少个,向后延伸比它小的有多少个。即该元素是处于山谷。
那么如何用单调栈维护这个呢?
首先这个栈是单调递增的,对于当前栈顶的元素top,如果比要进栈的元素now大,那么是top要出栈,那么top出栈后的栈顶元素suftop一定比top小,那么代表着suftop可以向后延伸到top的向后延伸位置,因为入栈元素now一定比top小,那么now可以向前延伸到top向前延伸的位置。
预处理一个前缀和,当元素出栈的时候就可以直接计算了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
#define N 100010
typedef long long LL;
struct node {
int pre, suf, id;
LL num;
} p[N];
LL sum[N]; void solve(int n) {
for(int i = ; i <= n; i++) {
scanf("%lld", &p[i].num);
p[i].pre = p[i].suf = ; p[i].id = i;
sum[i] = sum[i-] + p[i].num;
}
LL ans = -; int l, r; // 有0这个坑点
stack<node> sta;
sta.push(p[]);
for(int i = ; i <= n; i++) {
while(!sta.empty() && sta.top().num > p[i].num) {
node top = sta.top(); sta.pop();
if(!sta.empty()) sta.top().suf += top.suf;
p[i].pre += top.pre;
LL res = sum[top.id + top.suf - ] - sum[top.id - top.pre];
res *= top.num;
if(res > ans) {
ans = res;
l = top.id - top.pre + ;
r = top.id + top.suf - ;
}
}
sta.push(p[i]);
}
while(!sta.empty()) {
node top = sta.top(); sta.pop();
if(!sta.empty()) sta.top().suf += top.suf;
LL res = sum[top.id + top.suf - ] - sum[top.id - top.pre];
res *= top.num;
if(res > ans) {
ans = res;
l = top.id - top.pre + ;
r = top.id + top.suf - ;
}
}
printf("%lld\n%d %d\n", ans, l, r);
} int main() {
int n;
while(~scanf("%d", &n)) solve(n);
return ;
}
POJ 2796:Feel Good(单调栈)的更多相关文章
- poj 2796 Feel Good单调栈
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20408 Accepted: 5632 Case T ...
- poj 2796 Feel Good 单调栈区间问题
Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...
- POJ 3658 Artificial Lake (单调栈)
题意: 析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度. 代码如下: #pragma comment(linker, "/S ...
- poj 2559 Largest Rectangle(单调栈)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26549 ...
- POJ - 2796 Feel Good 单调递增栈+前缀和
Feel Good Bill is developing a new mathematical theory for human emotions. His recent investigations ...
- POJ 3415 后缀数组+单调栈
题目大意: 给定A,B两种字符串,问他们当中的长度大于k的公共子串的个数有多少个 这道题目本身理解不难,将两个字符串合并后求出它的后缀数组 然后利用后缀数组求解答案 这里一开始看题解说要用栈的思想,觉 ...
- poj 2796 Feel Good 单调队列
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8753 Accepted: 2367 Case Ti ...
- [poj 2796]单调栈
题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...
- POJ 2796 Feel Good 【单调栈】
传送门:http://poj.org/problem?id=2796 题意:给你一串数字,需要你求出(某个子区间乘以这段区间中的最小值)所得到的最大值 例子: 6 3 1 6 4 5 2 当L=3,R ...
随机推荐
- debian 下py2.7 安装mysql模块
先安装pip 然后用pip安装 setuptools 安装模块的时候会报错 python setup.py install sh: mysql_config: command not found Tr ...
- MVC基架生成的Edit视图
@model MyMusicStore.Models.Album @{ ViewBag.Title = "Edit"; } <h2>Edit</h2> ...
- WPF 附加属性的使用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- Post ,Get 请求
http://blog.csdn.net/pan_junbiao/article/details/9155497
- wget(1.11.4) for win
下载wget(1.11.4) for win 安装 添加wget环境变量,这样使用就更方便了,右键计算机->属性->高级系统设置->高级->环境变量->选中PATH-&g ...
- 零元学Expression Blend 4 - Chapter 37 看如何使用Clip修出想要的完美曲线(上)
原文:零元学Expression Blend 4 - Chapter 37 看如何使用Clip修出想要的完美曲线(上) 几何外部的 UIElement 会在呈现的配置中以视觉化方式裁剪. 几何不一定要 ...
- WPF使用WindowChrome实现自定义标题框功能
代码: <Window x:Class="WpfDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx ...
- PHP 的魔术方法及其应用
PHP中将所有__(两个下划线)开头的类方法作为魔术方法,这方法之所以称为魔术方法是因为其实现的功能就如变魔术一样感觉很神奇.在特定的事件下触发,这真的很酷. **__construct()** 这个 ...
- 中国目前的房地产总市值占GDP的比例为411%,远远高于世界平均水平的260%
到2015年统计,一线城市空置率22%,二线城市24%.中央开始喊要供应侧改革了. 中国目前的房地产总市值占GDP的比例为411%,远远高于世界平均水平的260%.无疑已经蕴含着比较明显的泡沫. 那么 ...
- BuildWinRTL.dproj 用这个重新编译就行
BuildWinRTL.dproj 用这个重新编译就行 我每次安装新版本,都删掉了这两个函数 {$IFDEF DEBUG}exports dbkFCallWrapperAddr,{$IF defin ...