HDU5461 Largest Point 贪心

题目链接: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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值