Feel Good
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 13376   Accepted: 3719
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

Source


题意:非负整数,求一个区间,区间和*区间最小值 最大

本题思想就是找以每个点为最小值的最长区间(也就是最大了)
找的方法就是单调栈
维护一个数值的递减栈,h数值,l是这个区间的和,b是开始位置(也就是向左延伸到的位置)
 
对于第i个数,先向左找,遇到更大的就用它更新答案并弹出它(因为它不可能再向右延伸了,最右到i-1)
然后插入这个数,并且插入的位置就是这个数最左的位置了
这个过程维护一个right,表示向右延伸的和
 
最后栈里的都是最右到n的
//
// main.cpp
// poj2796
//
// Created by Candy on 10/5/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=1e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n;
ll a,ans=-,ansb,anse;
struct data{
ll h,l;
int b;
}st[N];
int top=;
int main(int argc, const char * argv[]) {
n=read();
for(int i=;i<=n;i++){
a=read();
ll right=;int b=i;
while(top&&st[top].h>=a){
ll tmp=(st[top].h*(st[top].l+right));
right+=st[top].l;
b=st[top].b;
if(tmp>ans){
ans=tmp;
ansb=st[top].b;
anse=i-;//printf("t %lld %lld %lld\n",ans,ansb,anse);
}
top--;
}
top++;
st[top].h=a;
st[top].l=right+a;
st[top].b=b;
}
ll right=;
while(top){
ll tmp=(st[top].h*(st[top].l+right));
right+=st[top].l;
if(tmp>ans){
ans=tmp;
ansb=st[top].b;
anse=n;
}
top--;
}
printf("%lld\n%lld %lld",ans,ansb,anse);
return ;
}

POJ2796Feel Good[单调栈]的更多相关文章

  1. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  2. BZOJ 4453: cys就是要拿英魂![后缀数组 ST表 单调栈类似物]

    4453: cys就是要拿英魂! Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 90  Solved: 46[Submit][Status][Discu ...

  3. BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2326  Solved: 1054[Submit][Status ...

  4. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  5. bzoj1510: [POI2006]Kra-The Disks(单调栈)

    这道题可以O(n)解决,用二分还更慢一点 维护一个单调栈,模拟掉盘子的过程就行了 #include<stdio.h> #include<string.h> #include&l ...

  6. BZOJ1057[ZJOI2007]棋盘制作 [单调栈]

    题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...

  7. 洛谷U4859matrix[单调栈]

    题目描述 给一个元素均为正整数的矩阵,上升矩阵的定义为矩阵中每行.每列都是严格递增的. 求给定矩阵中上升子矩阵的数量. 输入输出格式 输入格式: 第一行两个正整数n.m,表示矩阵的行数.列数. 接下来 ...

  8. POJ3250[USACO2006Nov]Bad Hair Day[单调栈]

    Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17774   Accepted: 6000 Des ...

  9. CodeForces 548D 单调栈

    Mike and Feet Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Subm ...

随机推荐

  1. 推荐10个适合初学者的 HTML5 入门教程

    HTML5 作为下一代网站开发技术,无论你是一个 Web 开发人员或者想探索新的平台的游戏开发者,都值得去研究.借助尖端功能,技术和 API,HTML5 允许你创建响应性.创新性.互动性以及令人惊叹的 ...

  2. go语言最新版本 下载地址

    国内官方网站无法打开.放在了百度云中,定期会更新: 链接:http://pan.baidu.com/s/1dD59duh 密码:46ek 备用地址:http://pan.baidu.com/s/1hq ...

  3. 在android用Get方式发送http请求

    烦人的日子终于过去啦,终于又可以写博客啦,对自己的android学习做个总结,方便以后查看...... 一.在android用Get方式发送http请求,使用的是java标准类,也比较简单. 主要分以 ...

  4. swift 2.2 语法 (中)

    前言: 1.此文中的语法会根据Swift的升级变动而更新. 2.如果需要请移步 -> swift2.2 语法(上).swift 2.2语法(下) 函数 和C语言一样,swift也有函数,性质和我 ...

  5. iOS集成ijkplayer视频直播框架,遇到的bug和坑...

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 32.0px "Helvetica Neue"; color: #555555 } p. ...

  6. IOS开发之代理的设计小技巧

    1.关于代理对象的设计小技巧 在设计一个类,需要通过代理和协议来从外部获取需要的动态的数据.那么在这里设计使用代理会有两种方法. <第一种方法> 也是比较常见的: 在你设计的类中,声明一个 ...

  7. eclipse编码格式设置

    大家好,我是小Alan,很高兴大家能够看到这篇小小的技术点文章,这还是从参加工作以来,小Alan写的第一篇博文.喜欢能够给一些朋友带来方便. 说到eclipse编码格式的设置其实一个非常非常小的事情, ...

  8. 从DACPAC文件中读取元数据

    SQL数据库项目生成时会生成dacpac文件,可从中读出所需的元数据,进行一些转换(如生成数据字典)   var model = new TSqlModel(@"D:\kljob\CardL ...

  9. 哭瞎!360云盘将关停,你的几十T照片和文件该怎么办

    IDO老徐刚得到了一个非常不开心的消息,360云盘将停止个人云盘服务...进行业务转型,在网盘存储.传播内容的合法性和安全性得到彻底解决之前不再考虑恢复,之后转型企业云服务. 而且之前共享的所有资料, ...

  10. ORACLE数据库的限制

    ORACLE数据库最多可以拥有多少个表空间(Tablespace)?数据库最多拥有多少个数据文件(Database files).数据库的数据文件最大可以多大?遇到这些问题只能查询官方文档,人的记忆能 ...