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
Output
Sample Input
6
3 1 6 4 5 2
Sample Output
60
3 5 中文题意:
给定一个含有N个正整数的数组,我们定义一个区间[L,R]的平衡值为这个区间的数值和*这个区间的最下值。
让求出这个数组的平衡值最大的区间,并输出区间的边界。 思路:
定义两个数组L和R,L[i]表示从a[i]向左遍历,第一个比a[i]小的右边那个数的下标。
R[i] 表示从a[i]向右遍历,第一个比a[i]小的左边的那个数的下标。
以上的L和R数组,都可以利用单调栈进行O(N)求出,。然后因为要用到区间和,所以预处理一下前缀和数组sum即可。
然后我们扫一遍数组,以a[i]为最小值的区间的平衡值就为 : a[i]*( sum[r[i]]-sum[l[i]-1] )
然后对应求出最大值和区间情况。 代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int l[maxn];
int r[maxn];
ll sum[maxn];
int a[maxn];
int n;
int main()
{
gg(n);
repd(i,,n)
{
gg(a[i]);
sum[i]=sum[i-]+1ll*a[i];
}
stack<int> s;
// 1 2 3 4 5
//
while(s.size())
s.pop();
repd(i,,n)
{
while(s.size()&&a[s.top()]>=a[i])
{
s.pop();
}
if(s.size())
{
l[i]=s.top()+;
}else
{
l[i]=;
}
s.push(i);
}
while(s.size())
s.pop();
// 3 1 6 4 5 2
// 4 -> 5 2
int x;
for(int i=n;i>=;i--)
{
while(s.size()&&a[s.top()]>=a[i])
{
s.pop();
}
if(s.size())
{
r[i]=s.top()-;
}else
{
r[i]=n;
}
s.push(i);
}
ll ans=-;
int lf,ri;
repd(i,,n)
{
ll cnt=1ll*a[i]*(sum[r[i]]-sum[l[i]-]);
if(cnt>ans)
{
ans=cnt;
lf=l[i];
ri=r[i];
}
}
printf("%lld\n",ans);
printf("%d %d",lf,ri);
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}