[原创]POJ 2796 Feel Good 【单调栈】
[原创]POJ 2796 Feel Good 【单调栈】
2017-01-07 01:11:20 Tabris_ 阅读数:412
博客爬取于2020-06-14 22:42:26
以下为正文
版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/54150706
题目链接:http://poj.org/problem?id=2796
----------------------------------------------------------------------------------------------------.
Feel Good
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 13624 Accepted: 3807
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
Northeastern Europe 2005
----------------------------------------------------------------------------------------------------.
题目大意:
给定一个序列,让你求[]的最大值。
解题思路:
如果暴力求解的话很明显需要来解决,但是更明显的就是会超时.
于是换一种想法,由于我是找的单调栈类型题,所以,就没有想算法…
至于单调栈怎么维护,其实很简单了,
于其类似的有单调队列,
单调,单调,所以栈中所有元素都是递增的,
那么我们在维护一个单调栈的时候要将所有的于即将进栈的元素不单调的元素出栈,这样下去就能维护一个单调栈了.
维护单调栈的好处也很明显,能够快速的维护一个区间内的最小值.
这样下来在进行统计的时候就能减少很多不必要的计算.
首先区间和用一个前缀和来表示,将求取区间和的部分降到,在每一次维护一个元素的时候,采用一个结构体,
1 | struct node |
在每一次出栈操作的时候维护一下所需要计算的结果就行.
并不是很难想,但是注意元素全部为0的时候,初始化ans需要<0
附本题代码
1 | //#include <bits/stdc++.h> |