Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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 day was. 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.
The input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line.
The first line of the input file contains n<tex2html_verbatim_mark> - the number of days of Bill‘s life he is planning to investigate (1n100000)<tex2html_verbatim_mark> . The rest of the file contains n<tex2html_verbatim_mark> integer numbers a1, a2,..., an<tex2html_verbatim_mark> ranging from 0 to 106<tex2html_verbatim_mark> - the emotional values of the days. Numbers are separated by spaces and/or line breaks.
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
On the first line of the output file print the greatest value of some period of Bill‘s life.
On the second line print two numbers l<tex2html_verbatim_mark> and r<tex2html_verbatim_mark> such that the period from l<tex2html_verbatim_mark> -th to r<tex2html_verbatim_mark> -th day of Bill‘s life (inclusive) has the greatest possible value. If there are multiple periods with the greatest possible value, then print the shortest one. If there are still several possibilities, print the one that occurs first..
6 3 1 6 4 5 2
60 3 5
对于某个最小值ai来说,所选的区间应该尽量大,直到再选就不能保证ai是最小值的时候停止。
在扫描过程中维护一个向前延伸的最大位置,扩展的时候注意传递性,如果前面一个元素比它小,那么前面一个元素能延伸到的位置,
当前元素也可以延伸到,然后类似链表往前找的同时延伸链即可。向后找的时候类似。区间和用一个前缀和来处理。
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5+5; typedef long long ll; int a[maxn],l[maxn],r[maxn],n; ll sum[maxn]; int main() { int T = 0; sum[0] = 0; a[0] = -1; while(~scanf("%d",&n)){ if(T++) putchar(‘\n‘); a[n+1] = -1; for(int i = 1; i <= n; i++) scanf("%d",a+i),sum[i] = sum[i-1]+a[i],l[i]=r[i]=i; for(int i = 1; i <= n; i++){ while(a[i]<=a[l[i]-1]) l[i] = l[l[i]-1]; } for(int i = n; i >= 1; i--){ while(a[i]<=a[r[i]+1]) r[i] = r[r[i]+1]; } ll Max = (ll)a[1]*a[1]; int L = 1,R = 1; for(int i = 1; i <= n; i++){ ll tmp = (sum[r[i]]-sum[l[i]-1])*a[i]; if(tmp > Max || (tmp == Max && R - L > r[i] - l[i] )){ Max = tmp; L = l[i]; R = r[i]; } } printf("%lld\n%d %d\n",Max,L,R); } return 0; }
原文:http://www.cnblogs.com/jerryRey/p/4719690.html