Feel Good POJ - 2796 (前缀和+单调栈)(详解)
A new idea Bill has recently developed assigns a non-negative integer value to each day of human life.
Bill calls this value the emotional value of the day. The greater the emotional value is, the better the daywas. Bill suggests that the value of some period of human life is proportional to the sum of the emotional values of the days in the given period, multiplied by the smallest emotional value of the day in it. This schema reflects that good on average period can be greatly spoiled by one very bad day.
Now Bill is planning to investigate his own life and find the period of his life that had the greatest value. Help him to do so.
Input
Output
Sample Input
6
3 1 6 4 5 2
Sample Output
60
3 5 中文题意:
给定一个含有N个正整数的数组,我们定义一个区间[L,R]的平衡值为这个区间的数值和*这个区间的最下值。
让求出这个数组的平衡值最大的区间,并输出区间的边界。 思路:
定义两个数组L和R,L[i]表示从a[i]向左遍历,第一个比a[i]小的右边那个数的下标。
R[i] 表示从a[i]向右遍历,第一个比a[i]小的左边的那个数的下标。
以上的L和R数组,都可以利用单调栈进行O(N)求出,。然后因为要用到区间和,所以预处理一下前缀和数组sum即可。
然后我们扫一遍数组,以a[i]为最小值的区间的平衡值就为 : a[i]*( sum[r[i]]-sum[l[i]-1] )
然后对应求出最大值和区间情况。 代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int l[maxn];
int r[maxn];
ll sum[maxn];
int a[maxn];
int n;
int main()
{
gg(n);
repd(i,,n)
{
gg(a[i]);
sum[i]=sum[i-]+1ll*a[i];
}
stack<int> s;
// 1 2 3 4 5
//
while(s.size())
s.pop();
repd(i,,n)
{
while(s.size()&&a[s.top()]>=a[i])
{
s.pop();
}
if(s.size())
{
l[i]=s.top()+;
}else
{
l[i]=;
}
s.push(i);
}
while(s.size())
s.pop();
// 3 1 6 4 5 2
// 4 -> 5 2
int x;
for(int i=n;i>=;i--)
{
while(s.size()&&a[s.top()]>=a[i])
{
s.pop();
}
if(s.size())
{
r[i]=s.top()-;
}else
{
r[i]=n;
}
s.push(i);
}
ll ans=-;
int lf,ri;
repd(i,,n)
{
ll cnt=1ll*a[i]*(sum[r[i]]-sum[l[i]-]);
if(cnt>ans)
{
ans=cnt;
lf=l[i];
ri=r[i];
}
}
printf("%lld\n",ans);
printf("%d %d",lf,ri);
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Feel Good POJ - 2796 (前缀和+单调栈)(详解)的更多相关文章
- poj 2796 Feel Good单调栈
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20408 Accepted: 5632 Case T ...
- Java性能分析之线程栈详解与性能分析
Java性能分析之线程栈详解 Java性能分析迈不过去的一个关键点是线程栈,新的性能班级也讲到了JVM这一块,所以本篇文章对线程栈进行基础知识普及以及如何对线程栈进行性能分析. 基本概念 线程堆栈也称 ...
- 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 ...
- 2019南昌邀请赛预选赛 I. Max answer (前缀和+单调栈)
题目:https://nanti.jisuanke.com/t/38228 这题题解参考网上大佬的. 程序的L[i],R[i]代表a[i]这个点的值在区间 [L[i],R[i]] 中最小的并且能拓展到 ...
随机推荐
- Vue2 学习笔记5
文中例子代码请参考github watch属性的使用 考虑一个问题:想要实现 名 和 姓 两个文本框的内容改变,则全名的文本框中的值也跟着改变:(用以前的知识如何实现???) 监听data中属性的改变 ...
- MySQL8.0关于caching_sha2_password Plugin的一个Bug
今天在调试使用ansible进行标准化安装MySQL8.0时,发现关于caching_sha2_password plugin的一个bug. 在搭建主从复制时,按照手册说明需要创建用户: create ...
- 远程桌面连接一台关联无线的电脑(A)时,A电脑无线总是断开导致远程桌面连接失败
1. 我的环境: 两台电脑,分别记为PC1和PC2,PC1有线或者无线连在路由器上,PC2无线连在同一个路由器上.(当然,我的PC1是win10系统,PC2是win7系统) 2. PC1只要一远程连 ...
- Ubuntu下vim打开文件时,提示请按ENTER或其它命令继续
最近配置了一下vim,重启后,配置生效.但在用vim打开文件的时候,出现了一个问题:每次用vim打开文件的时候,都会提示请按ENTER或其它命令继续.这个真的很烦人.那么怎么把它消除掉呢? 首先要搞清 ...
- js获取当前页面url网址信息
js如何准确获取当前页面url网址信息 在WEB开发中,时常会用到javascript来获取当前页面的url网址信息,在这里是我的一些获取url信息的小总结. 下面我们举例一个URL,然后获得它的各个 ...
- June 15. 2018 Week 24th Friday
If at first you don't succeed, then dust yourself off and try again. 失败了没关系,重振旗鼓,从头再来. From Aaliyah, ...
- npm方法
1. 使用npm 下载全局包 npm install 包名字 -g 安装 npm uninstall 包名字 -g 卸载 2. 安装卸载本地的包 (在哪里执行命令就把包安装在哪个目录的node_mod ...
- 在Ubuntu上安装Jenkins
先决条件 安装Java SDK sudo apt-get install openjdk-8-jdk # sudo apt-get install openjdk-7-jdk 早些系统可以安装 第1步 ...
- 关于CUDA,cuDNN,TF,CUDA驱动版本兼容问题
实际工作当中,经常维护好几个项目的代码,不同项目依赖的TF版本不一致问题.网上找了好多资料,但是每次遇到的问题都不一样,每次都要去查(就是是一样的问题,解决办法也可能会不一样)每次踩坑无数,今天痛定思 ...
- [tool] Visual Studio Code python配置
语言设置 安装中文插件即可成为中文 选择一个Python解释器 Python是一种解释型语言,为了运行Python代码并获取Python IntelliSense,您必须告诉VS Code使用哪个解释 ...