2019牛客暑期多校训练营(第四场)C.sequence

2019牛客暑期多校训练营(第四场)C.sequence

题目链接

题目描述

Your are given two sequences a 1 … n a_{1 \dots n} a1n and b 1 … n b_{1 \dots n} b1n.You need to answer max ⁡ 1 ≤ l ≤ r ≤ n { m i n ( a l … r ) × s u m ( b l … r ) } \displaystyle \max_{1 \le l \le r \le n} \{min(a_{l \dots r}) \times sum(b_{l \dots r})\} 1lrnmax{min(alr)×sum(blr)}

Where min(a) means the minimal value of every element of sequence a, sum(a) means the sum of every element of sequence a .

输入描述:

The first line contains an integer n .

The second line contains n integers meaning a 1 … n a_{1 \dots n} a1n.

The third line contains n integers meaning b 1 … n b_{1 \dots n} b1n.

输出描述:

An integer meaning the answer.

示例1

输入

3
1 -1 1
1 2 3

输出

3

这题可以直接遍历求答案,对 a [ i ] > 0 a[i]>0 a[i]>0 时,要使 s u m sum sum 尽可能大,如果 a [ i ] < 0 a[i]<0 a[i]<0,就要使 s u m sum sum 尽可能小,对于小于 0 的情况,我们可以将所有数取反再算一次,避免遗漏最大值,AC代码如下:

#include
using namespace std;
typedef long long ll;
const int N=3e6+5;
int n;
ll a[N],b[N];
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++) scanf("%lld",&a[i]);
    for(int i=0;i<n;i++) scanf("%lld",&b[i]);
    ll sum=b[0],ans=a[0]*b[0],now=a[0];
    for(int i=1;i<n;i++){
        ans=max(ans,b[i]*a[i]);
        if(sum<0){
            sum=b[i],now=a[i];
        }else{
            sum+=b[i];
            now=min(now,a[i]);
            ans=max(ans,now*sum);
        }
        a[i]*=-1,b[i]*=-1;
    }
    sum=b[0],now=a[0];
    for(int i=1;i<n;i++){
        if(sum<0){
            sum=b[i],now=a[i];
        }else{
            sum+=b[i];
            now=max(now,a[i]);
            ans=max(ans,now*sum);
        }
    }
    printf("%lld",ans);
}

你可能感兴趣的:(思维,暴力,牛客)