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 ;
}

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

POJ2796 Feel Good -- 单调队列的更多相关文章

  1. POJ2796 单调队列

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

  2. BestCoder Round #89 B题---Fxx and game(单调队列)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945     问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路:  B ...

  3. 单调队列 && 斜率优化dp 专题

    首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...

  4. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

  5. BZOJ 1047 二维单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...

  6. 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列

    第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...

  7. BZOJ1047: [HAOI2007]理想的正方形 [单调队列]

    1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2857  Solved: 1560[Submit][St ...

  8. hdu 3401 单调队列优化DP

    Trade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  9. 【转】单调队列优化DP

    转自 : http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列是一种严格单调的队列,可以单调递增,也可以单调递减.队 ...

随机推荐

  1. FreeRTOS_软件定时器

    FreeRTOS 软件定时器 实验 创建2个任务,start_task.timercontrol_task. start_stask:创建timercontrol_task任务:创建周期定时器Auto ...

  2. CSS之常见文字样式整理

    常见文字样式 行高:line-height,当我i们将行高的大小设置成当前元素的高度时,可以实现当行文本在当前元素中垂直方向居中显示的效果 水平对齐方式:text-align:left|center| ...

  3. Luogu P5008 逛庭院

    题目传送门 我校神仙出的神仙题 \(\%\%\%\) 30分 找出所有有入度的点,排序,选前\(k\)个点,好了,30分到手. #include<iostream> #include< ...

  4. CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第七节

    第七节:使用下一代CUDA硬件,快乐加速度 原文链接 Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Laboratory)的高级科研人员.他在多个 ...

  5. C#Aspose操作Word & Excel简版(后会研究补充更多功能)

    利用Aspose操作Word & Excel首先要在项目中标引用Aspose.Words.dll和Aspose.Cells.dll. 首先说一说向Word中写入数据,目前做的是向Word中的标 ...

  6. Webpack--模块打包器

    首先介绍一个安装webpack的百度经验:https://jingyan.baidu.com/article/a3a3f811230ee58da3eb8a6e.html 推荐一个详细介绍webpack ...

  7. ajax 的 promise

    $.when().done().fail() $.when($.ajax("test1.html"),$.ajax("test2.html")).done(fu ...

  8. madplay移植

    移植前需求准备: a. 源码包: 1. libid3tag-0.15.1b.tar.gz 2. libmad-0.15.1b.tar.gz 3. madplay-0.15.2b.tar.gz 4. z ...

  9. tab菜单的点击的动态效果和内容页面的关联显示jQuery

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. ethtool查看网卡以及修改网卡配置

    ethtool 命令详解 命令描述: ethtool 是用于查询及设置网卡参数的命令. 使用概要:ethtool ethx       //查询ethx网口基本设置,其中 x 是对应网卡的编号,如et ...