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 exactlyT
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
注意题目中i!=j 的条件
AC代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
struct number
{
long long shu,xulie;
}x[500005],y[500005];
int cmp(number k,number l)
{
return k.shu<l.shu;
}
int main()
{
long long c,t,n,a,b,i,temp;
scanf("%lld",&t);
c=0;
while(t--)
{
scanf("%lld%lld%lld",&n,&a,&b);
for(i=0;i<n;i++)
{
scanf("%lld",&temp);
x[i].shu=a*temp*temp;
x[i].xulie=i;
y[i].shu=b*temp;
y[i].xulie=i;
}
sort(x,x+n,cmp);
sort(y,y+n,cmp);
printf("Case #%lld: %lld\n",++c,(x[n-1].xulie==y[n-1].xulie)?(max(x[n-1].shu+y[n-2].shu,x[n-2].shu+y[n-1].shu)):(x[n-1].shu+y[n-1].shu));
}
return 0;
}
本文介绍了一个算法问题,即从给定序列中选择两个不同的元素,通过特定公式计算最大值。文章提供了一段AC代码实现,该算法首先对元素进行排序,并巧妙地处理了选取元素时的特殊情况。
6万+

被折叠的 条评论
为什么被折叠?



