Manthan, Codefest 17 B

来源:互联网 发布:telnet登录端口 编辑:程序博客网 时间:2024/06/01 10:12

B. Marvolo Gaunt’s Ring
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt’s Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made.

Value of x is calculated as maximum of p·ai + q·aj + r·ak for given p, q, r and array a1, a2, … an such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative.

Input
First line of input contains 4 integers n, p, q, r ( - 109 ≤ p, q, r ≤ 109, 1 ≤ n ≤ 105).

Next line of input contains n space separated integers a1, a2, … an ( - 109 ≤ ai ≤ 109).

Output
Output a single integer the maximum value of p·ai + q·aj + r·ak that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n.

Examples
input
5 1 2 3
1 2 3 4 5
output
30
input
5 1 2 -3
-1 -2 -3 -4 -5
output
12
Note
In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30.

In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
树状数组区间求最值

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <vector>#include <string>using namespace std;int n;long long P[123456],Q[123456],R[123456];long long Y[123456];long long h[123456*3];long long k[123456*3];int lowbit(int x){    return (x&-x);}void update1(int x){    while(x<=n)    {        h[x]=P[x];        for(int i=1;i<lowbit(x);i<<=1)        h[x]=max(h[x],h[x-i]);        x+=lowbit(x);    }    return ;}void update2(int x){    while(x<=n)    {        k[x]=Y[x];        for(int i=1;i<lowbit(x);i<<=1)        k[x]=max(k[x],k[x-i]);        x+=lowbit(x);    }    return ;}long long find_max(int l,int r,long long a[],long long h[]){    long long ans=(long long)(-3*1e18-100000);    while(r>=l){        ans=max(ans,a[r]);        r--;        for(;r-lowbit(r)>=l;r-=lowbit(r)){            ans=max(ans,h[r]);        }    }    return ans;}int main(){    for(int i=1;i<=(int)(1e5+10);i++){        h[i]=(long long)(-3*1e18-100000);        k[i]=(long long)(-3*1e18-100000);        Y[i]=(long long)(-3*1e18-100000);    }    long long p,q,r;    scanf("%d %I64d %I64d %I64d",&n,&p,&q,&r);    for(int i=1;i<=n;i++){        long long a;        scanf("%I64d",&a);        P[i]=a*p;        Q[i]=a*q;        R[i]=a*r;    }    for(int i=1;i<=n;i++){        update1(i);    }    for(int i=1;i<=n;i++){        long long ma=find_max(1,i,P,h);        Y[i]=ma+Q[i];    }    for(int i=1;i<=n;i++){        update2(i);    }    long long Max=(long long)(-3*1e18-100000);    for(int i=1;i<=n;i++){        long long ma=find_max(1,i,Y,k);        Max=max(Max,find_max(1,i,Y,k)+R[i]);    }    cout<<Max<<endl;    return 0;}
原创粉丝点击