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
题意:给定数 n , a, b ,还有接下来的n个ti 的值,求解的最大的值;
分析: 因为 i 和 j 不可以取相等的值,所以将 和 的值分别贪心来求最大的值 ,如果 和 最大值中 i 和 j
的下标相等,则就判断第二大
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long int
#define maxn 1000010
typedef struct nodee{
ll x,y;
}node;
node maze[maxn],num[maxn];
bool check(node u,node v)
{
return u.y<v.y;
}
int main()
{
int f,countt=1;
ll n,a,b,z;
scanf("%d",&f);
while(f--){
scanf("%lld %lld %lld",&n,&a,&b);
for(int i=0;i<n;i++){
scanf("%lld",&z);
maze[i].x=i+1;
maze[i].y=a*z*z;
num[i].x=i+1;
num[i].y=b*z;
}
printf("Case #%d: ",countt);
countt++;
sort(maze,maze+n,check);
sort(num,num+n,check);
if(maze[n-1].x!=num[n-1].x){
printf("%lld\n",maze[n-1].y+num[n-1].y);
continue;
}
ll sum,ans;
sum=maze[n-1].y+num[n-2].y;
ans=maze[n-2].y+num[n-1].y;
printf("%lld\n",max(sum,ans));
}
return 0;
}