Feel Good

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8041   Accepted: 2177
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
 
运用单调队列,求出每个数值最左边和最右边能够达到的范围,满足该数值为最小即可。
 
实现不必像我下面多弄出一个循环,只需一次循环即可
每次元素入栈时,就可更新该元素的范围左端点
每次元素出栈时,就可更新该元素的范围右端点
 
#include <cstdio>
#include <deque>
#include <iostream> using namespace std; int a[],l[],r[];
long long sum[]; struct Type{
int res,data;
}; int main()
{
int n;
scanf("%d",&n);
for(int i=; i<=n; i++){
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
} deque<Type> p;
p.clear();
for(int i=; i<=n; i++){
while(!p.empty()&&p.back().data>=a[i])
p.pop_back();
if(p.empty()) l[i]=; else l[i]=p.back().res+;
Type t;
t.res=i;
t.data=a[i];
p.push_back(t);
} p.clear();
for(int i=n; i>=; i--){
while(!p.empty()&&p.back().data>=a[i])
p.pop_back();
if(p.empty()) r[i]=n; else r[i]=p.back().res-;
Type t;
t.res=i;
t.data=a[i];
p.push_back(t);
} // for(int i=1; i<=n; i++)
// printf("%d %d\n",l[i],r[i]); long long ans=;
int ansi;
for(int i=; i<=n; i++)
if(ans<=(sum[r[i]]-sum[l[i]-])*a[i]){
ans=(sum[r[i]]-sum[l[i]-])*a[i];
ansi=i;
} printf("%I64d\n",ans);
printf("%d %d\n",l[ansi],r[ansi]); return ;
}
 

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

  1. POJ2796 Feel Good -- 单调队列

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

  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. bnuoj 33656 J. C.S.I.: P15(图形搜索题)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=33656 [题解]:暴力搜索题 [code]: #include <iostream> # ...

  2. MyEclipse2015破解版_MyEclipse 2015 stable 2.0 稳定版 破解日志

    前言:在MyEclipse 2015 Stable 1.0下载安装破解日志(http://www.cnblogs.com/wql025/p/5161979.html)一文中,笔者主要讲述了该版本的破解 ...

  3. oracle——session

    一.解释session web应用中,session是服务器段保存用户信息的一个对象,cookie是浏览器端保存用户信息的对象.今天了解了oracle也有session对象,那么什么是oracle的s ...

  4. 完全卸载Oracle方法

    手动卸载 软件环境: 1.Windows XP + Oracle 10g 2.Oracle安装路径为:d:\Oracle 1.如果数据库配置了自动存储管理(ASM),应该先删除聚集同步服务CSS(cl ...

  5. C# Socket服务器端如何判断客户端断开

    使用Socket类中的Poll方法,就可以. Socket client //假如已经创建好了,连接到服务器端得Socket的客户端对象. 我们只要client.Poll(10,SelectMode. ...

  6. Web App之一

    JSP/HTML/CSS---------View(不包含任何的数据,只作为基本的layout) JS------------------------Data(update JSP/HTML)

  7. 6 个基于 jQuery 的表单向导插件推荐

    表单向导可以很好地引导用户进行一步一步的操作,从而降低用户错误输入的几率.尽管互联网中有大量的类似插件,但真正好用的不多. 本文整理了6个比较优秀的表单向导插件,希望能够为你带来帮助. 1. Smar ...

  8. First Lua function running in C

    这是我在C里面跑出来的第一个Lua 文件, 纪念一下. 1.Set up envirnonment: Mac下面 Lua的src (即include) 和lib(binary)是分开的, 所以需要分别 ...

  9. lua语言入门之Sublime Text设置lua的Build System

    转自: http://blog.csdn.net/wangbin_jxust/article/details/8911956 最近开始学习LUA语言,使用Sublime Text作为编辑器,不得不说, ...

  10. hdu 4187 Alphabet Soup

    这题的主要就是找循环节数,这里用找字符串最小覆盖来实现,也就是n-next[n],证明在这http://blog.csdn.net/fjsd155/article/details/6866991 #i ...