Feel Good
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 14287 | Accepted: 3950 | |
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.
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 10
6 - 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
题意:给你一列数,然后让你求出一个区间总和*区间最小值的最大值
思路:首先明确大致思路,枚举每个数,求以这个数为最小值的区间和*该最小值,枚举完之后求出最大值即可
但是确定范围肯定是不能暴力的,肯定会超时的,这里有两种做法,一种使用线段树(找到为该数为最小值的最小值得区间的最左端的数和最右端的数),二是回溯,求出以i-1为最小值的区间最左端,先与i-1比较如果大于i-1,可以结束了,如果小于i-1,那么久直接与s[i-1]-1比较,一直走下去,求最右端也是这个思路,不过是从后往前算罢了
ac代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
int a[100005];
int b[100005];
int c[100005];
long long sum[100005];
int main()
{
freopen("feelgood.in ","r",stdin);
freopen("feelgood.out","w",stdout);
int n;
while(cin>>n)
{
sum[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
long long max=-1;
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
a[0]=-1;a[n+1]=-1;
for(int i=1;i<=n;i++)
{
for(int j=i-1;j>=0;)
{
if(a[i]>a[j])
{b[i]=j+1;break;}
else
{
j=b[j]-1;
}
}
}
for(int i=n;i>=1;i--)
{
for(int j=i+1;j<=n+1;)
{
if(a[i]>a[j])
{
c[i]=j-1;break;
}
else
j=c[j]+1;
}
}
int x,y;
for(int i=1;i<=n;i++)
{
long long ans=(sum[c[i]]-sum[b[i]-1])*a[i];
if(ans>max)
{
max=ans;
x=b[i];y=c[i];
}
}
cout<<max<<endl;
cout<<x<<" "<<y<<endl;
}
return 0;
}