您的位置:首页 > 其它

HDU5461 Largest Point 贪心

2015-09-20 11:02 176 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5461

题目大意:给出一个数组t0,t1,...,tn-1,和整数a,b,找出a*ti^2+b*tj(i!=j)的最大值。

分析:对于数组中的每一个t,我们用两个数组A和B分别纪录a*ti^2和b*ti,然后对这两个数组排序,如果两个数组最大值的下标不同,那么相加就是最大值了,如果相同,那么就拿A数组的次大值加上B数组的最大值,和A数组的最大值加上B数字的次大值这两个值相比较,较大的即为最终的最大值。

实现代码如下:

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: