POJ2796 Feel Good 单调栈

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情

题目要我们求出一个区间这个区间的和与这个区间中最小的数的乘积值要最大。

对于每一个数字我们求出两个值,分别是lst[i],nxt[i],代表在[lst[i],nxt[i]]区间内a[i]最小。

得到这个区间的方法就是单调栈,单调栈中保存数字的位置。按照次序依次入栈,每次入栈的时候与栈顶比较如果小于栈顶则将栈顶元素出栈,并且更新栈顶元素的nxt域(因为当前元素是第一个比栈顶元素小的数字),然后知道栈为空,或者栈顶元素比a[i]小,然后按情况更新lst[i]即可。

然后比较求值即可。


Feel Good
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 8041 Accepted: 2177
Case Time Limit: 1000MS Special Judge

Description

Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life. 

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

The first line of the input contains n - the number of days of Bill's life he is planning to investigate(1 <= n <= 100 000). The rest of the file contains n integer numbers a1, a2, ... an ranging from 0 to 106 - the emotional values of the days. Numbers are separated by spaces and/or line breaks.

Output

Print the greatest value of some period of Bill's life in the first line. And on the second line print two numbers l and r such that the period from l-th to r-th day of Bill's life(inclusive) has the greatest possible value. If there are multiple periods with the greatest possible value,then print any one of them.

Sample Input

6
3 1 6 4 5 2

Sample Output

60
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>

using namespace std;

#define MAXN 100100

int n,s[MAXN],tp;
int a[MAXN],lst[MAXN],nxt[MAXN];
long long preSum[MAXN];
long long ans;
int al,ar;

void getPos(int i)
{
    lst[i]=i;nxt[i]=-1;
    while(tp!=0 && a[s[tp-1]]>a[i])
        nxt[s[--tp]]=i-1;
    s[tp++]=i;
    if(tp==1) lst[i]=1;
    else lst[i]=s[tp-2]+1;

}


int main()
{
    while(~scanf("%d",&n))
    {
        preSum[0]=0;tp=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            preSum[i]=preSum[i-1]+a[i];
            getPos(i);
        }
        ans=0;
        al=ar=1;
        for(int i=1;i<=n;i++)
        {
            int l=lst[i],r=nxt[i];
            if(r==-1) r=n;
            long long tmp=1;
            tmp=a[i];
            tmp*=(preSum[r]-preSum[l-1]);
            if(tmp>ans)
            {
                ans=tmp;
                al=l,ar=r;
            }
        }
        printf("%lld\n%d %d\n",ans,al,ar);
    }
    return 0;
}


poj 2796 Feel Good 单调栈题解 单调栈模板题#include <bits/stdc++.h> using namespace::std;typedef long long ll; const ll MAXN = 100000+50; ll ms[MAXN], r; ll L[MAXN], R[MAXN]; ll n, pos, maxNum, x[MAXN]; ll sum[MAXN];int main() { ios_b 阅读详情

相关推荐

poj2796Feel Good(单调栈)

Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13572   Accepted: 3796 Case Time Limit: 1000MS   Special Judge Description Bill is developin

fouzhe的博客 605

POJ 2796 Feel Good单调栈

题目链接:http://poj.org/problem?id=2796—————————————————————————————————-. Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 13624 Accepted: 3807 Case Time Limit: 1000MS

Tabris的博客 625

[poj 2796 Feel Good] 单调栈

[poj 2796 Feel Good] 单调栈题目链接:[poj 2796 Feel Good] 单调栈 题意描述:给定N个数a1,a2,…,an(1≤N≤105,0≤ai≤106)a_1,a_2,\ldots,a_n(1\le N\le 10^5, 0\le a_i\le 10^6),求区间最小的数*区间和。 解题思路: 单调栈: 顾名思义就是在入栈时遵循单调原则,可以求出一个元素向左(

Fighting! 1460

poj2796Feel Good 单调栈

//一个长度为n的序列,对于一个区间中的value为这个区间的最小值乘上这个区间的所有数的和 //这就是找每一个点,找以其为最小值的区间 //显然直接暴力法找必然超时 //可以开一个单调递增栈,对于每一个新元素a[i] //当前值小于等于栈顶元素,表示以栈顶元素为最小值的区间的右端是i //栈顶元素a[j]出栈 , 由于是单调递增栈,新的栈顶元素a[k]必然小于a[j],故需要更新新的栈

ijbuhv的专栏 4931

POJ2796 Feel Good 题解 单调栈

题目链接:http://poj.org/problem?id=2796【题目描述】《人类情感研究》 special judge比尔正在为人类情感研究发展一种新的数学理论。他最近的研究重点在于好的日子和坏的日子是如何在一段时期内影响人的记忆力的。比尔提出了一个想法,他为人们生活的每一天都赋予了一个非负整数值。比尔将这个值称为一天中的情感价值。一天的情感价值越大,人们在这一天就会过得越好。比尔认为,人...

weixin_30706507的博客 208

POJ 2796 Feel Good(单调栈)

题目地址:POJ 2796 单调栈的第一题就是这道。。把我弄的晕头转向。现在终于明白了,对单调栈又加深了理解。原来单调栈不只是可以维护数。 代码如下: #include #include #include #include #include #include #include #include #include #include #include #include u

scf0920 1031

POJ 2796 Feel Good 单调栈

最近做这方面的题很多,感觉单调栈很是神奇,通过维护一个单调不减的栈,遇到小于栈顶的栈顶元素便出栈,此时跟新结果。 这个比RMQ的ST算法要快很多,ST算法要枚举每一个区间(没想到其他做法,可能要dp,反正不会)。这个题关键在于给每一个点找一个最长子序列,使其最小值为其本身,这样扫一遍得到结果。而单调栈正好可以解决这个问题。 (这个题大家注意要用long long我被int卡死,干脆全换long

1292765944的专栏 1825

POJ-2796Feel Good单调栈

题意: 给出一个序列,长度为 nnn。定义区间价值为区间和*区间最小值,求出这个序列中的最大区间价值。(n≤105,0≤ai≤106)(n\leq 10^5,0\leq ai\leq 10^6)(n≤105,0≤ai≤106) 思路: 此题题意十分简洁,求区间价值,并且区间价值定义就是区间和*区间最小值。因此此题最直接的思路就是枚举区间,然后发现 n2n^2n2 算法不可行,然后考虑枚举最小值,...

lucius-liu.cn 713

POJ2796 Feel Good——单调栈

题目链接 题目大意 给你 nnn 个数,求一个区间和区间的值,定义区间的值为区间内所有数之和乘以区间最小值,输出区间的值以及区间的左右端点 解题思路 我们考虑每个数作为所在区间的最小值时,这个区间的左右端点是什么。 这时,我们可以正反跑两次单调栈,分别得到这个数作为区间最小值时,这个区间的左右端点。 考虑正向跑单调栈求区间左端点。维护一个上升的单调栈 如果当前元素比栈顶大,直接push进去,这个数的区间左端点就是栈顶元素的下标+1 如果当前元素小于等于栈顶元素,一直pop栈直到满足当前元素比栈顶大,然后取栈

yezi_coder的博客 208

单调栈-POJ 2796 Feel Good

POJ 2796 Feel Good 题目意思: \(\displaystyle \sum^j_{y=i}X_y, (1 \leq i\leq j \leq N)* min(X_{i}|1 \leq i \leq N)\) PS:这题要多组读入,C++ cout cin记得优化 #include <iostream> #include <stack> usin...

weixin_30666753的博客 120

Feel Good POJ 2796 单调栈

题意:求所有区间的最小值思路:维护一个单调递增栈 , 利用单调栈求出以当前数a[i] 为最小值 的区间的长度,每次更新最小值。点击打开链接#include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;stack&gt; #include&lt;cstring&gt; #define read(x) scanf("%lld",&amp;x) ...

qq_41231363的博客 199

POJ 2796 Feel Good单调栈

题目链接 Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some perio...

不二君 205

POJ2796 Feel Good(单调栈)

Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people’s memories about some period of l...

riba2534的博客 823

单调栈POJ 2796Feel Good

单调栈入门。 将每个当做区间最小值来扩展。怎么扩展?构造一个单调递减栈。即从栈顶元素到栈底元素,值是单调递减的,即栈顶元素的值始终是栈的最大值。然后每一个值有属于自己的区间,这个区间目的是为了记录之后的元素向前延伸的用处。 先将操作再将效果。 当扫描的元素大于栈顶元素时,直接入栈。 当扫描的元素等于栈顶元素时,不记录,只将区间延伸到后面。 当扫描的元素小于栈顶元素时,将栈顶弹出记录之前的值作为区间...

weixin_43331783的博客 161

POJ 2796Feel Good 单调栈经典题

Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11626   Accepted: 3212 Case Time Limit: 1000MS   Special Judge Description Bill is develop

4772

poj 2796 Feel Good 单调栈

J - Feel Good Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people’s memories about some period of ...

DaNIelLAk的博客 262
上一篇: SDOI2011 染色 动态树
下一篇: AHOI2009 Seq 维护序列seq 线段树
Albafica
博客等级 码龄15年 98粉丝 359原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值