Largest Point
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
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 .
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 .
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
Source
题意:给你一个序列t1,t2,…,tn以及系数a,b,从序列的n个元素中选取两个元素ti和tj(i≠j)使得值最大,并输出这个最大值
要使值最大,无非受a,b的正负性所影响,然而直接从暴力选取两个数找最大值的话,妥妥是超时的
然而,我们可以缩小暴力的数的范围,在此,我取了至多8个数,即负的最小的两个数,正的最大的两个数,以及最接近于0的4个数(接近于0的数的用处在于a为负数时,ti要尽可能接近0),当然你可以只取6个数,接近于0的数可以只取2个,然后暴力一下也就8*8的可能
要做bestcoder了,暂时先这样了,有问题可以问哦
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 5000005;
const __int64 inf = 100000000000000;
const int mod = 2009;
__int64 s[N],w[8];
int main()
{
int t,i,j,k=1,n,p;
__int64 Max,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d%I64d%I64d",&n,&a,&b);
for(p=i=0;i<n;i++)
scanf("%I64d",&s[i]);
sort(s,s+n);
if(s[0]*s[n-1]<0)
{
for(i=0;i<n&&s[i]<=0;i++);
if(i<=4)
for(j=0;j<i;j++)
w[p++]=s[j];
else
w[p++]=s[0],w[p++]=s[1],w[p++]=s[i-2],w[p++]=s[i-1];
if(n-i<=4)
for(j=i;j<n;j++)
w[p++]=s[j];
else
w[p++]=s[i],w[p++]=s[i+1],w[p++]=s[n-2],w[p++]=s[n-1];
}
else
{
if(n<=4)
for(j=0;j<n;j++)
w[p++]=s[j];
else
w[p++]=s[0],w[p++]=s[1],w[p++]=s[n-2],w[p++]=s[n-1];
}
/*for(i=0;i<p;i++)
printf("%I64d ",w[i]);
puts("");*/
for(i=0,Max=-inf;i<p;i++)
for(j=0;j<p;j++)
{
if(i==j)
continue;
if(Max<a*w[i]*w[i]+b*w[j])
Max=a*w[i]*w[i]+b*w[j];
}
printf("Case #%d: %I64d\n",k++,Max);
}
return 0;
}
菜鸟成长记