传送门

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

思路

定义一串数字,其子区间的值为子区间元素和乘以区间最小值,因此利用单调栈的思想,以每个当前值为区间最小值,向左向右拓展区间。如果当前元素大于栈顶元素,那么就不能往前拓展,
如果当前元素小于栈顶元素,这个时候就要把栈中的元素一个一个弹出来,直到当前元素大于栈顶元素,对于弹出来的元素,它扩展到当前元素就不能向后伸展下去了,因此对于弹出来的元素这个时候就可以计算左右端点形成区间和与最小值的乘积了,维护一个最大值即可。
 
#include<stdio.h>
#include<string.h>
typedef __int64 LL;
const int maxn = 1000005;
LL a[maxn],stack[maxn] = {0},left[maxn] = {0},sum[maxn] = {0};

int main()
{
	LL N,L = 0,R = 0,res = -1,tmp = 0;
	scanf("%I64d",&N);
	for (int i = 1;i <= N;i++)	scanf("%I64d",&a[i]),sum[i] = a[i] + sum[i-1];
	a[++N] = -1;    //确保栈中元素能被全部弹出
	int top = 0;
	for (int i = 1;i <= N;i++)
	{
		if (!top || a[i] > a[stack[top-1]])
		{
			stack[top++] = i;
			left[i] = i;
			continue;
		}

		if (a[i] == a[stack[top-1]])	continue;

		while (top > 0 && a[i] < a[stack[top-1]])
		{
			top--;
			tmp = a[stack[top]]*(sum[i-1] - sum[left[stack[top]] - 1]);
			if (tmp > res)	res = tmp,L = left[stack[top]],R = i-1;
		}
		tmp = stack[top];
		stack[top++] = i;
		left[i] = left[tmp];
	}
	printf("%I64d\n%I64d %I64d\n",res,L,R);
	return 0;
}

  

POJ 2796 Feel Good(单调栈)的更多相关文章

  1. poj 2796 Feel Good单调栈

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

  2. poj 2796 Feel Good 单调栈区间问题

    Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4 ...

  3. POJ 3658 Artificial Lake (单调栈)

    题意: 析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度. 代码如下: #pragma comment(linker, "/S ...

  4. poj 2559 Largest Rectangle(单调栈)

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

  5. POJ - 2796 Feel Good 单调递增栈+前缀和

    Feel Good Bill is developing a new mathematical theory for human emotions. His recent investigations ...

  6. POJ 3415 后缀数组+单调栈

    题目大意: 给定A,B两种字符串,问他们当中的长度大于k的公共子串的个数有多少个 这道题目本身理解不难,将两个字符串合并后求出它的后缀数组 然后利用后缀数组求解答案 这里一开始看题解说要用栈的思想,觉 ...

  7. poj 2796 Feel Good 单调队列

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

  8. [poj 2796]单调栈

    题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...

  9. POJ 2796:Feel Good(单调栈)

    http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://ww ...

  10. POJ 2796 Feel Good 【单调栈】

    传送门:http://poj.org/problem?id=2796 题意:给你一串数字,需要你求出(某个子区间乘以这段区间中的最小值)所得到的最大值 例子: 6 3 1 6 4 5 2 当L=3,R ...

随机推荐

  1. java文件cmd运行出现中文乱码

    今天刚开始学java,使用cmd命令执行java文件的时候,发现中文打出来是一串乱码. 于是就百度了一下,发现一个行之有效的方法. 首先使用命令:javac -encoding utf-8 Hello ...

  2. jquery总结

    id选择器只能选定第一个满足条件的元素 class选择器可以选定一类满足条件的元素 text(),html(),val(),attr()等操作类型的函数,作用对象是前面选择器选定的元素.选定的元素可能 ...

  3. 一个奇妙的java坑:Long 类型的比较

    Long userId=127L; Long authorId=127L; System.out.println(userId==authorId);//true userId=128L; autho ...

  4. android studio用法笔记

    1.每次创建新工程的时候,就会“check sdk repository”,然而这个进度条一动不动,解决方法:file>setting>plugins>sdk updater禁用就行 ...

  5. C#开发命名规范

    学习C#之初,始终不知道怎么命名比较好,很多时候无从命名,终于有一天我整理了一份命名规范文档,自此我就是按照这个命名规范书写代码,整洁度无可言表,拙劣之处请大家斧正,愚某虚心接受,如有雷同,不胜荣幸 ...

  6. void与void之间没有隐式转换(纯属恶搞,请勿在意)

    强大的vs弹出了这个提示:.有没有觉得强大的vs不应该出现该提示. 但就是出现了. 看客,您知道怎么让vs弹出这个提示吗^~^

  7. 1031MVCC和事务浅析

    转自 http://blog.csdn.net/sofia1217/article/details/50778906 关于MVCC浅析,有些难度http://xuebinbin212.blog.163 ...

  8. js浏览器窗口

    一.clientwidth和clientheight 注:返回了元素大小,但没有单位,默认单位是 px,如果你强行设置了单位,比如 100em之类,它还是会返回 px的大小. (CSS获取的话,是照着 ...

  9. bzoj1012

    Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L 个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. ...

  10. bash中不可以用字符串做数组下标

    bash中可以用字符串做数组下标吗例如 test["abc"]=1------解决方案-------------------- 好像是误会,是awk里可以,bash shell里不 ...