Largest Point
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 637 Accepted Submission(s): 266
Problem Description
Input
Output
Sample Input
2
3 2 1
1 2 3
5 -1 0
-3 -3 0 3 3
Sample Output
Source
2015 ACM/ICPC Asia Regional Shenyang Online
/*小技巧:
分析:对于数组中的每一个t,我们用两个数组A和B分别纪录a*ti^2和b*ti,
然后对这两个数组排序,如果两个数组最大值的下标不同,那么相加就是最大值了,
如果相同,那么就拿A数组的次大值加上B数组的最大值,
和A数组的最大值加上B数字的次大值这两个值相比较,较大的即为最终的最大值。
*/
//改进不用快排
#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <algorithm>
#include <cstdlib>
#include <math.h>
using namespace std;
const int N=5e+6+10;
struct Node{
long long v;
int id;
}num1,num2,num11,num22;
int main(){
int t,n,i;
long long a,b,z;
scanf("%d",&t);
int now=1;
while(t--){
long long re=0;
num1.v=-(5e+18);
num11.v=-(5e+18);
num2.v=-(5e+18);
num22.v=-(5e+18);
num1.id=0;
num11.id=0;
num2.id=0;
num22.id=0;
scanf("%d%lld%lld",&n,&a,&b);
for(i=0;i<n;i++){
scanf("%lld",&z);
if(a*z*z>num1.v){
num11.v=num1.v;
num11.id=num1.id;
num1.v=a*z*z;
num1.id=i;
}
else if(a*z*z>num11.v){
num11.v=a*z*z;
num11.id=i;
}
if(b*z>num2.v){
num22.v=num2.v;
num22.id=num2.id;
num2.v=b*z;
num2.id=i;
}
else if(b*z>num22.v){
num22.v=b*z;
num22.id=i;
}
}
if(num1.id!=num2.id)
re=num1.v+num2.v;
else
re=max(num1.v+num22.v,num11.v+num2.v);
printf("Case #%d: %lld\n",now++,re);
}
return 0;
}
/*超时
长得丑.......
#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <algorithm>
#include <cstdlib>
#include <math.h>
using namespace std;
const int N=5*1e6+10;
struct Node{
long long v;
int id;
bool operator <(const Node &b) const {
if(v==b.v)
return id<b.id;
return v<b.v;
}
}num1[N],num2[N];
int main(){
int t,n,i;
long long a,b,z;
scanf("%d",&t);
int now=1;
while(t--){
long long re=0;
scanf("%d%lld%lld",&n,&a,&b);
for(i=0;i<n;i++){
scanf("%lld",&z);
num1[i].id=i;
num2[i].id=i;
num1[i].v=a*z*z;
num2[i].v=b*z;
}
sort(num1,num1+n);
sort(num2,num2+n);
if(num1[n-1].id!=num2[n-1].id)
re=num1[n-1].v+num2[n-1].v;
else
re=max(num1[n-1].v+num2[n-2].v,num1[n-2].v+num2[n-1].v);
printf("Case #%d: %lld\n",now++,re);
}
return 0;
}
*/
/*
别人的代码951MS 过了 看脸啊.....日
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=5e6+10;
int n,a,b;
struct node
{
int id;
long long x;
bool operator <(const node &a) const
{
return x<a.x;
}
}A[maxn],B[maxn];
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
int t,T=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&a,&b);
for(int i=0;i<n;i++)
{
long long v;
scanf("%I64d",&v);
A[i].x=a*v*v;
B[i].x=b*v;
A[i].id=B[i].id=i;
}
sort(A,A+n);
sort(B,B+n);
printf("Case #%d: ",T++);
if(A[n-1].id!=B[n-1].id)
{
printf("%I64d\n",A[n-1].x+B[n-1].x);
continue;
}
printf("%I64d\n",max(A[n-1].x+B[n-2].x,A[n-2].x+B[n-1].x));
}
return 0;
}
*/