Problem Description
Given the sequence
A
with
n
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
at2i+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) and b (0≤|b|≤106) . The second line contains n integers t1,t2,⋯,tn where 0≤|ti|≤106 for 1≤i≤n .
The sum of n for all cases would not be larger than 5×106 .
For each test case, the first line contains three integers corresponding to n (2≤n≤5×106), a (0≤|a|≤106) and b (0≤|b|≤106) . The second line contains n integers t1,t2,⋯,tn where 0≤|ti|≤106 for 1≤i≤n .
The sum of n for all cases would not be larger than 5×106 .
Output
The output contains exactly
T
lines.
For each test case, you should output the maximum value of at2i+btj .
For each test case, you should output the maximum value of at2i+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
开两个数组维护一下
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=5e6+100;
struct node
{
__int64 num;
int index;
} aa[maxn],bb[maxn];
bool cmp(node x,node y)
{
if(x.num<y.num) return true;
if(x.num==y.num&&x.index<y.index) return true;
return false;
}
int main()
{
int i,j,t,n,a,b;
scanf("%d",&t);
__int64 ans,t1,t2;
for (int z=1;z<=t;z++) {
scanf("%d%d%d",&n,&a,&b);
for(i=0;i<n;i++) {
scanf("%I64d",&bb[i].num);
bb[i].index=i;
aa[i].num=bb[i].num*bb[i].num;
aa[i].index=i;
}
sort(aa,aa+n,cmp);
sort(bb,bb+n,cmp);
if(a==0) ans=max(b*bb[0].num,b*bb[n-1].num);
else if(b==0) ans=max(a*aa[0].num,a*aa[n-1].num);
else if(a<0) {
if(b<0) {
if(aa[0].index==bb[0].index) {
t1=a*aa[1].num+b*bb[0].num;
t2=a*aa[0].num+b*bb[1].num;
ans=max(t1,t2);
}
else ans=a*aa[0].num+b*bb[0].num;
}
else if(b>0){
if(aa[0].index==bb[n-1].index) {
t1=a*aa[1].num+b*bb[n-1].num;
t2=a*aa[0].num+b*bb[n-2].num;
ans=max(t1,t2);
}
else ans=a*aa[0].num+b*bb[n-1].num;
}
}
else if(a>0){
if(b<0) {
if(aa[n-1].index==bb[0].index) {
t1=a*aa[n-2].num+b*bb[0].num;
t2=a*aa[n-1].num+b*bb[1].num;
ans=max(t1,t2);
}
else ans=a*aa[n-1].num+b*bb[0].num;
}
else {
if(aa[n-1].index==bb[n-1].index) {
t1=a*aa[n-2].num+b*bb[n-1].num;
t2=a*aa[n-1].num+b*bb[n-2].num;
ans=max(t1,t2);
}
else ans=aa[n-1].num*a+b*bb[n-1].num;
}
}
printf("Case #%d: %I64d\n",z,ans);
}
return 0;
}