Given the sequence
A A
with n n
integers t 1 ,t 2 ,⋯,t n t1,t2,⋯,tn.
Given the integral coefficients
a a
and b b.
The fact that select two elements
t i ti
and t j tj
of A A
and i≠j i≠j
to maximize the value of at 2 i +bt j ati2+btj,
becomes the largest point.
For each test case, the first line contains three integers corresponding to n (2≤n≤5×10 6 ), a (0≤|a|≤10 6 ) n (2≤n≤5×106), a (0≤|a|≤106) and b (0≤|b|≤10 6 ) b (0≤|b|≤106). The second line contains n n integers t 1 ,t 2 ,⋯,t n t1,t2,⋯,tn where 0≤|t i |≤10 6 0≤|ti|≤106 for 1≤i≤n 1≤i≤n.
The sum of n n for all cases would not be larger than 5×10 6 5×106.
For each test case, you should output the maximum value of at 2 i +bt j ati2+btj.
2 3 2 1 1 2 3 5 -1 0 -3 -3 0 3 3
Case #1: 20
Case #2: 0
按照ab的符号分好四种情况就可以了
ac代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long long a1[5000005];
int main()
{
long long n,a,b,t;
cin>>t;
int p=0;
while(t--)
{
p++;
cin>>n>>a>>b;
cin>>a1[0];
long long max1=abs(a1[0]),min1=abs(a1[0]),max2=a1[0],min2=a1[0];
if(a<=0&&b<=0)
{
int k=0;
for(int i=1;i<n;i++)
{
scanf("%lld",&a1[i]);
if(abs(a1[i])<min1)
{min1=abs(a1[i]);k=i;}
}
int flag=0;
for(int i=1;i<n;i++)
{
if(a1[i]<min2&&i!=k)
{
min2=a1[i];
}
}
printf("Case #%d: %lld\n",p,a*min1*min1+b*min2);
}
if(a<=0&&b>0)
{
int k=0;
for(int i=1;i<n;i++)
{
scanf("%lld",&a1[i]);
if(abs(a1[i])<min1)
{min1=abs(a1[i]);k=i;}
}
for(int i=1;i<n;i++)
if(a1[i]>max2&&i!=k)
max2=a1[i];
printf("Case #%d: %lld\n",p,a*min1*min1+b*max2);
}
if(a>0&&b<=0)
{
int k=0;
for(int i=1;i<n;i++)
{
scanf("%lld",&a1[i]);
if(abs(a1[i])>max1)
{max1=abs(a1[i]);k=i;}
}
for(int i=1;i<n;i++)
{
if(a1[i]<min2&&i!=k)
min2=a1[i];
}
printf("Case #%d: %lld\n",p,a*max1*max1+b*min2);
}
if(a>0&&b>0)
{
int k=0;
for(int i=1;i<n;i++)
{
scanf("%lld",&a1[i]);
if(abs(a1[i])>max1)
{max1=abs(a1[i]);k=i;}
}
for(int i=1;i<n;i++)
{
if(a1[i]>max2&&i!=k)
max2=a1[i];
}
printf("Case #%d: %lld\n",p,a*max1*max1+b*max2);
}
}
return 0;
}