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. Linux下Oracle重启问题

    一.切换成oracle用户 $ su - oracle 注意:不要写成“su oracle ”不然是没办法使用下面的命令的. 如下所示:将会报:command not found的错误 二.使用命令 ...

  2. 微信 小程序 drawImage wx.canvasToTempFilePath wx.saveFile 获取设备宽高 尺寸问题

    以下问题测试环境为微信开发者0.10.102800,手机端iphone6,如有不对敬谢指出. 根据我的测试,context.drawImage,在开发者工具中并不能画出来,只有预览到手机中显示. wx ...

  3. SAP打印机配置

    SAP打印机配置 一.SAP打印原理 SAP的打印过程分两个步骤: 1.创建假脱机请求: 2.创建输出请求: 在点击打印按钮后,系统会提示创建假脱机请求后,你可以选择直接生成输出请求,或者手动生成输出 ...

  4. 操作系统开发系列—13.g.操作系统的系统调用 ●

    在我们的操作系统中,已经存在的3个进程是运行在ring1上的,它们已经不能任意地使用某些指令,不能访问某些权限更高的内存区域,但如果一项任务需要这些使用指令或者内存区域时,只能通过系统调用来实现,它是 ...

  5. No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386)

    今天在运行一个老ios项目的时候,突然报错:No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VAL ...

  6. swift 中手势的使用

    swift 中手势的使用 /**点击手势*/ func tapGestureDemo() { //建立手势识别器 let gesture = UITapGestureRecognizer(target ...

  7. 【C语言】C语言运算符

    目录: [算术运算符] [自增运算符] [自减运算符] [关系运算符] [逻辑运算符] [三目运算符] [sizeof运算符] · 作用 1.算术运算符 +(加).-(减).*(乘)./(除).%(取 ...

  8. 内存管理范围和@property

    管理范围: 管理任何继承NSObject的对象,对其他的基本数据类型无 效 本质原因是因为对象和其他数据类型在系统中的存储空间不一样,其它局部变量主要存放于 栈中,而对象存储于堆中,当代码块结束时这个 ...

  9. mac os下可能是最好的豆瓣电台——diumoo

    由于我一直用豆瓣fm听音乐,在网上找了下豆瓣的相关应用,都感觉不是太好, 最后发现一个mac版的app--diumoo! 这个软件看着非常舒服,一点也不占桌面空间,它一直默默在桌面右上角,鼠标划上去会 ...

  10. MVC模式与三层架构和表示层

    1.MVC模式     - Model-View-Controller     - 模型-视图-控制器     - Model(模型)         > 模型分为业务模型,和数据模型     ...