Description
Given the sequence
with
integers
. Given the integral coefficients
and
. The fact that select two elements
and
of
and
to maximize the value of
, becomes the largest point.
Input
An positive integer
, indicating there are
test cases.
For each test case, the first line contains three integers corresponding to and . The second line contains integers where for .
The sum of for all cases would not be larger than .
For each test case, the first line contains three integers corresponding to and . The second line contains integers where for .
The sum of for all cases would not be larger than .
Output
The output contains exactly
lines.
For each test case, you should output the maximum value of .
For each test case, you should output the maximum value of .
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,t1,...,tn-1,和整数a,b,
找出a*ti^2+b*tj(i!=j)的最大值。
分析:对于数组中的每一个t,我们用两个数组f1和f2分别纪录a*ti^2和b*ti,
然后对这两个数组排序,如果两个数组最大值的下标不同,那么相加就是最大值了,
如果相同,那么就拿f1数组的次大值加上f2数组的最大值,
和f1数组的最大值加上f2数组的次大值这两个值相比较,较大的即为最终的最大值。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const int maxn = 5e6 + 5;
const int mod = 1e8;
LL n, a, b, t;
struct Node
{
LL num, id;
}f1[maxn], f2[maxn];
int cmp(Node x, Node y)
{
return x.num > y.num;
}
int main(void)
{
// freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
int T, cas = 1, i, j;
scanf("%d", &T);
while (T--)
{
scanf("%I64d %I64d %I64d", &n, &a, &b);
for (i = 1; i <= n; i++){
scanf("%I64d", &t);
f1[i].num = a * t * t;
f2[i].num = b * t;
f1[i].id = f2[i].id = i;
}
sort(f1+1, f1+1+n, cmp);
sort(f2+1, f2+1+n, cmp);
printf("Case #%d: ", cas++);
if (f1[1].id != f2[1].id)
printf("%I64d\n", f1[1].num+f2[1].num);
else
printf("%I64d\n", max(f1[1].num+f2[2].num, f1[2].num+f2[1].num));
}
return 0;
}