Feel Good
Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 74 Accepted Submission(s) : 20
Special Judge
Problem 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<sup>6</sup> - 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<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define inf 0x7fffffff
int n,q[100005];
int a[100005],l[100005],r[100005];
ll sum[100005];
void get_left()
{
int i,head=1,tail=0;
for(i=1;i<=n;i++)
{
while(head<=tail&&a[q[tail]]>=a[i]) {l[i]=l[q[tail]];tail--;}
q[++tail]=i;
}
}
void get_right()
{
int i,head=1,tail=0;
for(i=n;i>=1;i--)
{
while(head<=tail&&a[q[tail]]>=a[i]) {r[i]=r[q[tail]];tail--;}
q[++tail]=i;
}
}
void get_max()
{
int i,low,righ;
ll cnt=-1,temp=0;
for(i=1;i<=n;i++)
{
temp=(ll)(sum[r[i]]-sum[l[i]-1])*a[i];
if(temp>cnt) {cnt=temp; low=l[i]; righ=r[i];}
}
printf("%I64d\n%d %d",cnt,low,righ);
}
int main()
{
int i; sum[0]=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
l[i]=r[i]=i;
}
get_left();
get_right();
get_max();
return 0;
}
其余方法有,创建递增单调队列,对于每个元素记录连续的比他大的最左边的下标,每当插入比他小的元素a[i],i-1就是连续比他大的最右边下标,计算出一次结果后,他出队,他的最左元素被继承。
参考:http://blog.youkuaiyun.com/clove_unique/article/details/50625545
http://blog.youkuaiyun.com/u010885899/article/details/49148025
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define pi 3.14159265358979
#define inf 0x7fffffff
int n,q[100005];
int a[100005],lef[100005];
ll sum[100005];
int main()
{
int i,low,high;
ll temp=0,ans=-1;
scanf("%d",&n);
sum[0]=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
a[++n]=-1;
int tail=0;
for(i=1;i<=n;i++)
{
if(tail==0||a[q[tail]]<a[i])
{
lef[i]=i;
q[++tail]=i;
continue;
}
if(a[q[tail]]==a[i]) continue;
while(tail>=1&&a[q[tail]]>a[i])
{
temp=(ll)1*a[q[tail]]*(sum[i-1]-sum[lef[q[tail]]-1]);
if(ans<temp)
{
ans=temp;
high=i-1;
low=lef[q[tail]];
//printf("%lld\n%d %d",ans,low,high);
}
tail--;
}
lef[i]=lef[q[tail+1]];
q[++tail]=i;
}
printf("%lld\n%d %d",ans,low,high);
return 0;
}
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define pi 3.14159265358979
#define inf 0x7fffffff
int n,q[100005];
int a[100005],lef[100005],rig[100005];
ll sum[100005];
int main()
{
int i,low,high;
ll temp=0,ans=-1;
scanf("%d",&n);
sum[0]=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
lef[i]=rig[i]=i;
sum[i]=sum[i-1]+a[i];
}
int tail=1; q[tail]=1;
for(i=2;i<=n;i++)
{
while(tail>=1&&a[q[tail]]>=a[i])
{
lef[i]=lef[q[tail]];
rig[q[tail-1]]=rig[q[tail]];
tail--;
}
rig[q[tail]]=rig[i];
q[++tail]=i;
}
while(tail>=1)
{
rig[q[tail-1]]=rig[q[tail]];
tail--;
}
for(i=1;i<=n;i++)
{
temp=(ll)(sum[rig[i]]-sum[lef[i]-1])*a[i];
if(ans<temp)
{
ans=temp;
low=lef[i];
high=rig[i];
}
}
printf("%I64d\n%d %d",ans,low,high);
return 0;
}