Given the sequence A with nn integers t1,t2,⋯,tn. Given the integral coefficients a and b. The fact that select two elements ti and tj of A and i≠j to maximize the value of ati+btj, becomes the largest point.
Input
An positive integer T, indicating there are T test cases.
For each test case, the first line contains three integers corresponding to n (2≤n≤5×106), a (0≤|a|≤106)n (2≤n≤5×106), a (0≤|a|≤106) and b (0≤|b|≤106)b (0≤|b|≤106). The second line contains nn integers t1,t2,⋯,tn where 0≤|ti|≤1060≤|ti|≤106 for 1≤i≤n1≤i≤n. The sum of nn for all cases would not be larger than 5×1065×106.
Output
The output contains exactly TT lines.
For each test case, you should output the maximum value of ati+btj.
Sample Input
2
3 2 1
1 2 3
5 -1 0
-3 -3 0 3 3
Sample Output
Case #1: 20
Case #2: 0
算法分析:
输入的a和b因为符号的不同,所要求的题ti,tj.比如当a大于0时,b也大于0时。a,b可能选取的数据是最大值,第二大值,和最大的负数。
比如当a大于0时,b小于0时 ,可能选取的数据是最大值,负数最大值或是最小正数。
比如当a小于0时,b大于0时 ,绝对值最小值,正数最大值。
比如当a小于0时,b小于0时,绝对值最小值,负数最大值或最小正数。
因为只需要找到这几个点,就可以比较了。
所以我的做法是把这些可能的点保存在另一个数组中,然后,暴力枚举法。
# include<cstdio>
# include<algorithm>
# include<cstring>
# include<iostream>
# define INF 1e18
using namespace std;
long long ans[5000005];
long long bns[100];
int main()
{
long long t,flag,k=1;
long long n,a,b,count1;
long long x,num;
scanf("%lld",&t);
while(t--)
{
num=-INF;
flag=-1;
count1=0;
scanf("%lld%lld%lld",&n,&a,&b);
for(int i=0; i<n; i++)
{
scanf("%lld",&ans[i]);
}
sort(ans,ans+n);
/*
for(int i=0;i<n;i++)
{
printf("%d ",ans[i]);
}
*/
for(int i=0; i<n; i++)
{
if(ans[i]>0)
{
flag=i;
break;
}
}
if(flag==0)
{
if(n<10)
{
for(int i=0; i<n; i++)
bns[count1++]=ans[i];
}
else
{
bns[count1++]=ans[0];
bns[count1++]=ans[1];
bns[count1++]=ans[n-2];
bns[count1++]=ans[n-1];
}
}
else if(flag<0)
{
if(n<10)
{
for(int i=0; i<n; i++)
bns[count1++]=ans[i];
}
else
{
bns[count1++]=ans[0];
bns[count1++]=ans[1];
bns[count1++]=ans[n-2];
bns[count1++]=ans[n-1];
}
}
else if(flag>0)
{
if(flag<10)
{
for(int i=0; i<flag; i++)
bns[count1++]=ans[i];
}
else
{
bns[count1++]=ans[0];
bns[count1++]=ans[1];
bns[count1++]=ans[flag-1];
bns[count1++]=ans[flag-2];
}
if((n-flag)<10)
{
for(int i=flag; i<n; i++)
bns[count1++]=ans[i];
}
else
{
bns[count1++]=ans[flag];
bns[count1++]=ans[flag+1];
bns[count1++]=ans[n-1];
bns[count1++]=ans[n-2];
}
}
/*
for(int i=0;i<count1;i++)
printf("%d ",bns[i]);
*/
for(int i=0; i<count1; i++)
{
for(int j=0; j<count1; j++)
{
if(i!=j)
{
x=a*bns[i]*bns[i]+b*bns[j];
if(num<x) num=x;
}
}
}
printf("Case #%lld: %lld\n",k++,num);
}
return 0;
}