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
Output
The output contains exactly T
lines.
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
我脑子里第一想法,求偏导数,然后找偏导数是0的点,然后就没有bei
所以在我的脑子里又出现了,(x,y)是一个二维平面, 然后找一个点的Z值最大,自己想到的只有 的算法。
今天看到题解发现,又是同一个问题,我们假设一个数全都能用,但是我们做标记,就是用了的话,第二个数就不能用了。
1.标记。 2.排序。3.思维。(不要只是看到x,y,要看到他们最后要比较的是 , 或者说是换元,来比较他们)
人的思维总是想着不重不漏,可是我们可以重复,然后标记啊
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;i++)
const int N=5e6+10;
struct node{
LL val;
int id;
bool operator<(const node& b)const{
return val<b.val;
}
}v1[N],v2[N];
int main()
{
int T;
scanf("%d",&T);
rep(kase,0,T){
int n,a,b;
scanf("%d %d %d",&n,&a,&b);
rep(i,0,n){
LL val;
scanf("%lld",&val);
v1[i].val=val*val*a;
v2[i].val=val*b;
v1[i].id=v2[i].id=i;
}
sort(v1,v1+n);
sort(v2,v2+n);
printf("Case #%d: ",kase+1);
if(v1[n-1].id!=v2[n-1].id)
printf("%lld\n",v1[n-1].val+v2[n-1].val);
else{
LL ans1=v1[n-1].val+v2[n-2].val;
LL ans2=v1[n-2].val+v2[n-1].val;
printf("%lld\n",max(ans1,ans2));
}
}
return 0;
}