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 10 6 - 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
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 (前缀和+单调栈)(详解)的更多相关文章

  1. poj 2796 Feel Good单调栈

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20408   Accepted: 5632 Case T ...

  2. Java性能分析之线程栈详解与性能分析

    Java性能分析之线程栈详解 Java性能分析迈不过去的一个关键点是线程栈,新的性能班级也讲到了JVM这一块,所以本篇文章对线程栈进行基础知识普及以及如何对线程栈进行性能分析. 基本概念 线程堆栈也称 ...

  3. poj 2796 Feel Good 单调栈区间问题

    Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...

  4. POJ 3658 Artificial Lake (单调栈)

    题意: 析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度. 代码如下: #pragma comment(linker, "/S ...

  5. poj 2559 Largest Rectangle(单调栈)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26549 ...

  6. POJ - 2796 Feel Good 单调递增栈+前缀和

    Feel Good Bill is developing a new mathematical theory for human emotions. His recent investigations ...

  7. POJ 3415 后缀数组+单调栈

    题目大意: 给定A,B两种字符串,问他们当中的长度大于k的公共子串的个数有多少个 这道题目本身理解不难,将两个字符串合并后求出它的后缀数组 然后利用后缀数组求解答案 这里一开始看题解说要用栈的思想,觉 ...

  8. poj 2796 Feel Good 单调队列

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8753   Accepted: 2367 Case Ti ...

  9. 2019南昌邀请赛预选赛 I. Max answer (前缀和+单调栈)

    题目:https://nanti.jisuanke.com/t/38228 这题题解参考网上大佬的. 程序的L[i],R[i]代表a[i]这个点的值在区间 [L[i],R[i]] 中最小的并且能拓展到 ...

随机推荐

  1. C# -- 随机数(Random)的使用

    使用随机数产生一组大乐透号码 1. C#代码 1 Console.WriteLine("===============大乐透===红色球==============="); Lis ...

  2. Leancloud+Valine打造Hexo个人博客极简评论系统

    以下配置是基于Next主题6.1.0版本 效果见个人博客的最下方评论. Leancloud配置 首先访问Leancloud官网https://leancloud.cn/ 有Github账号的小伙伴可以 ...

  3. LeetCode算法题-Remove Duplicates from Sorted List

    这是悦乐书的第160次更新,第162篇原创 01 前情回顾 昨晚的爬楼梯算法题,有位朋友提了个思路,使用动态规划算法.介于篇幅问题,这里不细说动态规划算法,以后会在数据机构和算法的理论知识里细说. 昨 ...

  4. spring cloud 实践之hystrix注意事项

    当我们写类似下面代码时 package demo1.demo1; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org ...

  5. June 9. 2018, Week 23rd, Saturday

    I know nothing except the fact of my ignorance. 除了自己的无知,我一无所知. Believe it or not, true wisdom exists ...

  6. Sudoku 小项目

    Sudoku 小项目 - 软工第二次作业 Part 1 · 项目相关 Github 地址: https://github.com/TheSkyFucker/Sudoku 项目的更多信息以及所有开发文档 ...

  7. 【Teradata】数据库初始化(sysinit和dip工具)

    1.删除数据库对象 (1)使用root用户登录数据库节点 arcmain .LOGON 127.0.0.1/dbc,dbc_password; (2)清理所有数据库对象及数据 DELETE DATAB ...

  8. C. Lorenzo Von Matterhorn LCA

    C. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. JWT 认证 以及Django 中的应用

    jwt 认证 私钥.公钥.CA认证 用一套加密规则 加密和解密 RSA加密 (非对称的加密) 摘要算法:MD5 FTP/互联网下载软件校验MD5 私钥 --RSA算法-->公钥 RSA原理 加密 ...

  10. Python学习之函数进阶

    函数的命名空间 著名的python之禅 Beautiful is better than ugly. Explicit is better than implicit. Simple is bette ...