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. js在一个div里面移动其子div

    var ChildDiv = $("#cid"); var width = 0; //鼠标点击子div的地方和子div的左边边距距离 var height = 0; //鼠标点击子 ...

  2. EOS签名R值过大导致报错"is_canonical( c ): signature is not canonical"

    简要 EOS中规定签名的R和S必须同时小于N/2才是合法的签名. 详细 EOS签名交易相对BTC和ETH来说,对签名的要求更加严格了. BTC中bip62规定了((Low S values in si ...

  3. git入门使用摘录

    无论使用github或者gitlab,第一步都是在本地生产ssh-key,ssh-key作为客户端的身份证存放在user用户的.ssh文件夹下.如果之前没有生产过,需要用ssh-keygen命令生成. ...

  4. HTTP 请求方法介绍

    浏览器从 web 服务器(或者叫应用服务器)上使用 HTTP 协议下载网站,HTTP 协议是基于一种 请求-响应(request-response)模型的.客户端(你的浏览器)从运行在物理机器上的 w ...

  5. springmvc如何获取参数

    请参考这篇文章, 写得比较全面. https://www.cnblogs.com/xiaoxi/p/5695783.html

  6. 从多个textarea中随机选取一个内容

    <div id="IMContentTest"> <textarea name="IMContent" class="IMClass ...

  7. Bootstrap 弹出框(Popover)插件

    Bootstrap 弹出框(Popover)插件与Bootstrap 提示工具(Tooltip)插件类似,提供了一个扩展的视图,用户只需要把鼠标指针悬停到元素上面即可.弹出框的内容完全由Bootstr ...

  8. C# checked运算符

    一.C# checked运算符 checked运算符用于对整型算术运算和显式转换启用溢出检查. 默认情况下,表达式产生的值如果超出了目标类型的范围,将会产生两种情况: ?常数表达式将导致编译时错误. ...

  9. 使用Xcode过程中遇到的问题

    前言:记录一下使用Xcode过程中遇到的问题 1.关于开发者的Team的问题,是选用自己的个人Team还是选用公司的付费的Team(本机环境:Xcode9 + iPad :iOS11.0.3) 问题: ...

  10. SpringBoot日志输出至Logstash

    1.springboot项目pom.xml文件下添加如下配置 2.resources目录下创建logback-spring.xml文件 <?xml version="1.0" ...