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.
Output The output contains exactly T lines.
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 |
给出t0-tn-1个数,和a,b,找出a*tx^2+b*ty的最大值,要求x!=y
这道题目数据有问题,根本没有500万个数,建一个50万的数组一样可以过
这里我们直接分别计算a*ti^2和b*ti,需要保持他们的序号,然后排序,去最大的两个,如果最大的两个序号相同,就
取 a*ti^2和b*ti分别第1大和第2大作比较,判断输出就可以了
AC:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define inf 9999999
#define maxn 5000007
using namespace std;
int t,n,cas=1;
long long q;
struct node
{
long long x;
int t;
}a[maxn],b[maxn];
bool cmp(node x,node y)
{
return x.x>y.x;
}
int main()
{
int cas=1,ai,bi;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&ai,&bi);
for(int i=0;i<n;i++)
{
scanf("%lld",&q);
a[i].x=q*q*ai;
a[i].t=i;
b[i].x=q*bi;
b[i].t=i;
}
printf("Case #%d: ",cas++);
sort(a,a+n,cmp);
sort(b,b+n,cmp);
if(a[0].t!=b[0].t)
printf("%lld\n",a[0].x+b[0].x);
else
printf("%lld\n",max((a[0].x+b[1].x),(a[1].x+b[0].x)));
}
return 0;
}