题目描述
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 becomes the largest point.
输入
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 .
输出
The output contains exactly T lines. For each test case, you should output the maximum value of
样例输入
2 3 2 1 1 2 3 5 -1 0 -3 -3 0 3 3
样例输出
Case #1: 20 Case #2: 0
题意:计算a*ti*ti+b*tj的最大值,且j != i
思路:分别计算a*t*t的值和b*t的值,并记录t的下标,比较两者最大值下标是否相等,不等直接输出结果,相等再比较次
最大值,输出结果。。。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef struct node
{
ll step;//记录下标
ll value;//记录值
}p;
p maxx[1000010],mbxx[1000010];//分别存a*t*t和b*t的值和下标
bool cmp(p a,p b)
{
return a.value>b.value;
}
int main()
{
int t;
int temp=1;
scanf("%d",&t);
while(t--)
{
ll n,a,b;
scanf("%lld%lld%lld",&n,&a,&b);
for(int i=0;i<n;i++)
{
ll x;
scanf("%lld",&x);
maxx[i].step=i;
maxx[i].value=a*x*x;
mbxx[i].step=i;
mbxx[i].value=b*x;
}
sort(maxx,maxx+n,cmp);
sort(mbxx,mbxx+n,cmp);
if(maxx[0].step!=mbxx[0].step)//判断最大值下标
printf("Case #%d: %lld\n",temp++,maxx[0].value+mbxx[0].value);
else
printf("Case #%d: %lld\n",temp++,maxx[0].value+mbxx[1].value>maxx[1].value+mbxx[0].value?maxx[0].value+mbxx[1].value:maxx[1].value+mbxx[0].value);
}
return 0;
}