POJ2796

来源:互联网 发布:thinkpad软件冲突蓝屏 编辑:程序博客网 时间:2024/05/17 02:55

1.题目描述:

Feel Good
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 14288 Accepted: 3951Case 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

63 1 6 4 5 2

Sample Output

603 5

Source

Northeastern Europe 2005
2.题意概述:

给定一个数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素。求该数组中的最大参考值以及对应的区间。

3.解题思路:

设某个区间所有元素的和为height,区间最小元素为width,则对于单个元素的区间,height = width = 元素的值。
可以想到建立一个单调递增的栈。从第一个元素开始入栈,每个元素入栈之前必须先从栈顶开始删除大于或等于它的元素,把删除的所有元素的height累加到当前元素的height,然后把当前元素的值保存在width值中,这表示把当前元素前面比它大或相等的连续元素的值加起来,乘以它自己,也就是这段区间的参考值。每一次删除元素都需要计算一个参考值,取参考值的最大值就是答案了。不过题目还要求给出对应区间的起点和终点,因此在栈的操作过程中还得记录当前元素保存的区间的起点和大小,在更新参考值的过程中顺便更新区间的起点和终点就可以了。

4.AC代码:

#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <functional>#include <cmath>#include <vector>#include <queue>#include <map>#include <set>#include <ctime>#define INF 0x7fffffff#define maxn 100100#define eps 1e-6#define pi acos(-1.0)#define e 2.718281828459#define mod (int)1e9 + 7;using namespace std;typedef long long ll;int a[maxn], stack[maxn], l[maxn];int n, top, ansl, ansr;ll ans, sum[maxn];int main(){#ifndef ONLINE_JUDGE  freopen("in.txt", "r", stdin);  freopen("out.txt", "w", stdout);  long _begin_time = clock();#endif  while (scanf("%d", &n) != EOF)  {    memset(stack, 0, sizeof(stack));    memset(sum, 0, sizeof(sum));    memset(l, 0, sizeof(l));    sum[0] = 0;    ans = -1;    for (int i = 1; i <= n; i++)    {      scanf("%d", &a[i]);      sum[i] = sum[i - 1] + a[i];    }    a[++n] = -1; // 尾部标记    top = 0;    for (int i = 1; i <= n; i++)    {      if (top == 0 || a[i] > a[stack[top - 1]]) //栈顶为空或者当前元素大于栈顶元素      {        stack[top++] = i; //入栈,保存元素下标        l[i] = i;         //保存左区间        continue;      }      if (a[i] == a[stack[top - 1]]) //相等则区间延伸        continue;      while (top >= 1 && a[i] < a[stack[top - 1]]) //小于则把当前大于元素弹出栈,并记录值      {        top--;        ll temp = a[stack[top]] * (sum[i - 1] - sum[l[stack[top]] - 1]); //求和        if (temp > ans)                                                  //大于则记录        {          ansl = l[stack[top]];          ans = temp;          ansr = i - 1;        }      }      l[i] = l[stack[top]];      stack[top++] = i;    }    printf("%lld\n%d %d\n", ans, ansl, ansr);  }#ifndef ONLINE_JUDGE  long _end_time = clock();  printf("time = %ld ms\n", _end_time - _begin_time);#endif  return 0;}

0 0