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.
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
题目大意:给出一系列的数t,给出a、b,找出最大的a*ti2+b*tj,其中,i<>j。
题目分析:将a*ti2和b*tj分别存放在两个数组中,排下序并找出最大的。若两个最大的下标不相同,则和即为答案;若相同,再找两个次大的,求来自不同数组的最大的加次大的的和,再取二和之中大的便是答案。
代码如下:
# include<iostream> # include<cstdio> # include<cstring> # include<vector> # include<map> # include<set> # include<list> # include<cstdlib> # include<string> # include<iomanip> # include<algorithm> using namespace std; # define LL long double struct arr { LL val; int id; arr(){} arr(LL a,int b):val(a),id(b){} bool operator < (const arr& a) const { return val<a.val; } }; arr w1[500005],w2[500005]; int main() { int T,a,b,n,cas=0; scanf("%d",&T); while(T--) { int k; scanf("%d%d%d",&n,&a,&b); for(int i=0;i<n;++i){ scanf("%d",&k); w1[i]=arr((LL)a*(LL)k*(LL)k,i); w2[i]=arr((LL)b*(LL)k,i); } sort(w1,w1+n); sort(w2,w2+n); printf("Case #%d: ",++cas); if(w1[n-1].id!=w2[n-1].id) cout<<fixed<<setprecision(0)<<w1[n-1].val+w2[n-1].val<<endl; else{ LL ans1=w1[n-1].val+w2[n-2].val; LL ans2=w1[n-2].val+w2[n-1].val; cout<<fixed<<setprecision(0)<<max(ans1,ans2)<<endl; } } return 0; }