Feel Good
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 14489   Accepted: 4015
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
3 5 题意:
  给出一个数列,求其一个区间中 最小值 与 区间元素和 的乘积的最大值;
分析:
  1.将每个元素看做所在区间的最小值 向左右两区间进行查找,找以其为最小值的最大区间;
  2.单调队列的应用,以查找以当前元素为最小值的最大区间的左端点为例:
   ①构造严格递增的单调队列,即进队元素需比队尾元素大,否则队尾元素出队;
   ②从第一个元素开始进行进队,将当前值与队尾进行比较,若队尾大于当前元素,则队尾出队,否则队尾元素的下标便是以当前元素为最小值
    的最大区间的左端点;
    查找以当前元素为最小值的最大区间的右端点方法相同。
代码分析:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include<string.h>
#include<stack>
#include<set>
#include <queue>
using namespace std; long long a[];
//数组模拟队列
long long q[];
//以每个元素为最小值的最大区间左端点
long long l[];
//以每个元素为最小值的最大区间右端点
long long r[];
//存队列中每个元素的下标
long long p[];
//区间的值
long long sum[];
int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
sum[] = ;
for(i = ; i<=n; i++)
{
scanf("%lld",a+i);
sum[i] = sum[i-]+a[i];
}
//初始化队头
q[] = -;
p[] = ;
p[n+] = n+;
q[n+] = -;
int head = ;
int tail = ;
//查找以当前元素为最小值的最大区间的左端点
for(i = ; i<=n; i++)
{ while(head<=tail&&q[tail]>=a[i]) tail--;//队尾元素大于等于当前元素
//以当前元素为最小值的最大区间的左端点
l[i] = p[tail];
//当前元素进队
q[++tail] = a[i];
//记录下标
p[tail] = i;
}
//查找以当前元素为最小值的最大区间的右端点
q[] = -;
p[] = ;
p[n+] = n+;
q[n+] = -;
head = n;
tail = n+;
for(i = n ; i>=; i--)
{
while(head>=tail&&q[tail]>=a[i]) tail++;
r[i] = p[tail];
q[--tail] = a[i];
p[tail] = i;
}
long long max1 = -;
int k = ; //标记最大值的区间
for(i = ; i<=n; i++)
{ if(max1<a[i]*(sum[r[i]-]-sum[l[i]]))
{
max1= a[i]*(sum[r[i]-]-sum[l[i]]);
k = i;
}
}
printf("%lld\n",max1);
printf("%lld %lld\n",l[k]+,r[k]-); }
return ;
}

个人随笔,望大佬勿喷,若能提供帮助不胜荣幸。

最新文章

  1. NYOJ 187
  2. 自动化测试selenium----css选择器总结
  3. 8. javacript高级程序设计-BOM
  4. Java并发学习之十九——线程同步工具之Phaser
  5. 2038: [2009国家集训队]小Z的袜子(hose) - BZOJ
  6. 查询可用的Nuget服务地址
  7. js中调用mangeto的js翻译
  8. 2015年十大热门Android开源新项目
  9. js脚本中try与cache捕获异常处理
  10. Spring集成Redis缓存
  11. js 移动端上拉加载下一页通用方案
  12. linux上创建svn服务器(centos7.3)
  13. redis_字符串对象
  14. intellj idea 跑覆盖率
  15. The stacking context
  16. sap 调试工具,修改变量值
  17. 判断窗体 show完成
  18. SpingBoot:Unregistering JMX-exposed beans on shutdown
  19. hadoop输出lzo文件并添加索引
  20. ubuntu查询命令行安装的软件的安装路径

热门文章

  1. bzoj1189 [HNOI2007]紧急疏散
  2. 【BZOJ3994】[SDOI2015] 约数个数和(莫比乌斯反演)
  3. Json的本地写入和读取,也可以方便在开发中数据的调试
  4. 使用OpenFileDialog组件打开对话框
  5. js当中mouseover和mouseout多次触发(非冒泡)
  6. 5- vue django restful framework 打造生鲜超市 -完成商品列表页(上)
  7. ECMAScript 6入门扩展笔记
  8. JZOJ 5184. 【NOIP2017提高组模拟6.29】Gift
  9. JZOJ 3509. 【NOIP2013模拟11.5B组】倒霉的小C
  10. 09.VUE学习之watch监听属性变化实现类百度搜索栏功能ajax异步请求数据,返回字符串