Largest Point
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
Given the sequence
A
with
n
integers
t
1
,t
2
,⋯,t
n![]()
. Given the integral coefficients
a
and
b
. The fact that select two elements
t
i![]()
and
t
j![]()
of
A
and
i≠j
to maximize the value of
at
2
i
+bt
j![]()
, 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×10
6
), a (0≤|a|≤10
6
)
and
b (0≤|b|≤10
6
)
. The second line contains
n
integers
t
1
,t
2
,⋯,t
n![]()
where
0≤|t
i
|≤10
6![]()
for
1≤i≤n
.
The sum of n
for all cases would not be larger than
5×10
6![]()
.
For each test case, the first line contains three integers corresponding to n (2≤n≤5×10
The sum of n
Output
The output contains exactly
T
lines.
For each test case, you should output the maximum value of at
2
i
+bt
j![]()
.
For each test case, you should output the maximum value of at
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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const long long N=5000010;
int num[N];
typedef struct AA
{
long long f;//保存ati^2
long long Count;//保存编号
} A;
A s[N];
typedef struct BB
{
long long ff;//保存btj
long long ccount;//保存编号
} B;
B ss[N];
int cmp(const void* x1,const void* y1)//ati^2降序
{
A* x=(A*)x1;
A* y=(A*)y1;
long long m=y->f-x->f;
return m;
}
int com(const void* x1,const void* y1)//btj降序
{
B* x=(B*)x1;
B* y=(B*)y1;
long long m=y->ff-x->ff;
return m;
}
int main()
{
long long c,n,a,b,i,cnt=1;
long long sum;
scanf("%d",&c);
while(c--)
{
sum=0;
scanf("%d%d%d",&n,&a,&b);
for(i=0; i<n; ++i)//输入处理保存数据
{
scanf("%d",&num[i]);
s[i].f=a*num[i]*num[i];
ss[i].ff=b*num[i];
s[i].Count=i;
ss[i].ccount=i;
}
qsort(s,n,sizeof(AA),cmp);//排序
qsort(ss,n,sizeof(BB),com);
if(s[0].Count==ss[0].ccount)//如果最大的两个数编号相同
{
if((s[0].f+ss[1].ff)>(s[1].f+ss[0].ff))
sum=s[0].f+ss[1].ff;
else
sum=s[1].f+ss[0].ff;
}
else
sum=s[0].f+ss[0].ff;
printf("Case #%d: %I64d\n",cnt++,sum);
}
return 0;
}
嗯,所有数据都要用long long。。