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

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<cmath> const int maxn=1e5+5;
typedef long long ll;
using namespace std;
ll a[maxn];
ll sum[maxn];
ll L[maxn];
ll L2[maxn];
int main()
{
int n;ll mn=0x3f3f3f3f;
while(cin>>n)
{
for(int t=1;t<=n;t++)
{
scanf("%lld",&a[t]);
mn=min(mn,a[t]);
}
memset(sum,0,sizeof(sum));
for(int t=1;t<=n;t++)
{
sum[t]=sum[t-1]+a[t];
}
stack<int> S1,S2;
while(!S1.empty())
{
S1.pop();
}
while(!S2.empty())
{
S2.pop();
} for(ll t=1;t<=n;t++)
{
while(S1.size() && a[S1.top()] >= a[t]) S1.pop();
if(S1.empty()) L[t] = 0;
else L[t] = S1.top();
S1.push(t);
}
for(ll t=n;t>=1;t--)
{
while(S2.size() && a[S2.top()] >= a[t]) S2.pop();
if(S2.empty()) L2[t] = n+1;
else L2[t] = S2.top();
S2.push(t);
}
ll maxnn=n*mn;//注意这个初始化,很细节
int j=1,k=n;
for(int t=1;t<=n;t++)
{
if((sum[L2[t]-1]-sum[L[t]])*a[t]>maxnn)
{
maxnn=(sum[L2[t]-1]-sum[L[t]])*a[t];
j=L[t]+1;
k=L2[t]-1;
}
}
cout<<maxnn<<endl;
cout<<j<<" "<<k<<endl;
}
return 0;
}

Feel Good(两遍单调栈维护区间+前缀和)的更多相关文章

  1. 【bzoj5089】最大连续子段和 分块+单调栈维护凸包

    题目描述 给出一个长度为 n 的序列,要求支持如下两种操作: A  l  r  x :将 [l,r] 区间内的所有数加上 x : Q  l  r : 询问 [l,r] 区间的最大连续子段和. 其中,一 ...

  2. bzoj1007: [HNOI2008]水平可见直线 单调栈维护凸壳

    在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的.例如,对于直线:L1:y=x; L2:y=-x; L3 ...

  3. [CSP-S模拟测试]:A(单调栈维护凸包+二分答案)

    题目传送门(内部题150) 输入格式 第一行两个整数$N,Q$. 接下来的$N$行,每行两个整数$a_i,b_i$. 接下来的$Q$行,每行一个整数$x$. 输出格式 对于每个询问,输出一行一个整数表 ...

  4. LOJ #2769 -「ROI 2017 Day 1」前往大都会(单调栈维护斜率优化)

    LOJ 题面传送门 orz 斜率优化-- 模拟赛时被这题送走了,所以来写篇题解( 首先这个最短路的求法是 trivial 的,直接一遍 dijkstra 即可( 重点在于怎样求第二问.注意到这个第二问 ...

  5. CF1137E Train Car Selection(单调栈维护凸函数)

    首先本题的关键是一次性加0操作只有第一个0是有用的.然后对于1 k操作,其实就是把之前的所有数删除.对于其他的情况,维护一次函数的和,将(i,a[i])看成平面上的一个点,用单调栈维护一下. #inc ...

  6. 翻转长方形 (不知名oj中一道个人私题)--单调栈维护最大子矩形

    怎么分析这道题呢? 首先 ,我们注意到一点: 不管怎么操作,任意一个2*2方格中的 "#"个数的奇偶性是不变的. 所以,如果一个2*2方格中有奇数个"#",这个 ...

  7. Lost My Music:倍增实现可持久化单调栈维护凸包

    题目就是求树上每个节点的所有祖先中(ci-cj)/(dj-di)的最小值. 那么就是(ci-cj)/(di-dj)的最大值了. 对于每一个点,它的(ci,di)都是二维坐标系里的一个点 要求的就是祖先 ...

  8. 【单调栈】【前缀和】【二分查找】8.28题解-long

    long 题目描述 AP神牛准备给自己盖一座很华丽的宫殿.于是,他看中了一块N*M的矩形空地.空地中每个格子都有自己的海拔高度.AP想让他的宫殿的平均海拔在海平面之上(假设海平面的高度是0,平均数都会 ...

  9. Q - Queue HDU - 5493(树状树组维护区间前缀和 + 二分找预留空位)

    Q - Queue HDU - 5493 Problem Description NNN people numbered from 1 to NNN are waiting in a bank for ...

随机推荐

  1. maven安装错误履历

    1\:maven cannot find entry:"/src/main/java" 先删除source下的文件夹 再新建文件夹

  2. Docker02 基本命令、开发环境搭建、docker安装nginx、Dockerfile、路径挂载

    1 基本命令 1.1 docker相关 centos6.5 安装docker环境 >sudo yum install -y http://mirrors.yun-idc.com/epel/6/i ...

  3. solr第一天 基础增删改查操作

    全文检索技术   Lucene&Solr               Part2 1 课程计划 1.索引库的维护 a) 添加文档 b) 删除文档 c) 修改文档 2.Lucene的查询 a)  ...

  4. PCL 平面模型分割

    点云操作中,平面的分割是经常遇到的问题,下面的例子就是如何利用PCL库提拟合出的参数,之后就可以过滤掉在平面附近的点云. #include <iostream> #include < ...

  5. Gnu C API使用指南

    1)posix_fadvise http://blog.yufeng.info/archives/1917 2)fts系列 http://www.cnblogs.com/patientAndPersi ...

  6. Vim编码知识,乱码问题

    原文:http://demi-panda.com/2012/12/26/vim-encoding/ 在vim的初始学习阶段,乱码经常是困扰新手的一个比较烦躁的问题,本文试图阐述Vim的编码知识,及设置 ...

  7. Oracle ERP系统借贷关系表

    Oracle ERP系统借贷关系表 成本核算会计信息归纳 按照事务处理的来源类型归纳. 一. 采购接收入库和退货: 1.接收:   借:材料采购 (订单价格) 贷:应计负债 (订单价格) 2.入库: ...

  8. MVC区域路由配置

  9. Stopwatch运行时间 Parallel并行任务

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  10. Android动态显示或隐藏密码框中的密码(Android学习笔记)

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...