hdu5461-Largest Point(贪心)

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 .

 

 

Output

The output contains exactly T lines.
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

 

      给出t0-tn-1个数,和a,b,找出a*tx^2+b*ty的最大值,要求x!=y

     这道题目数据有问题,根本没有500万个数,建一个50万的数组一样可以过

     这里我们直接分别计算a*ti^2和b*ti,需要保持他们的序号,然后排序,去最大的两个,如果最大的两个序号相同,就

     取     a*ti^2和b*ti分别第1大和第2大作比较,判断输出就可以了

    AC:

    

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define inf 9999999
#define maxn 5000007
using namespace std;

int t,n,cas=1;
long long q;

struct node
{
    long long x;
    int t;
}a[maxn],b[maxn];

bool cmp(node x,node y)
{
    return x.x>y.x;
}

int main()
{
    int cas=1,ai,bi;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&ai,&bi);
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&q);
            a[i].x=q*q*ai;
            a[i].t=i;
            b[i].x=q*bi;
            b[i].t=i;
        }
        printf("Case #%d: ",cas++);
        sort(a,a+n,cmp);
        sort(b,b+n,cmp);
        if(a[0].t!=b[0].t)
            printf("%lld\n",a[0].x+b[0].x);
        else
            printf("%lld\n",max((a[0].x+b[1].x),(a[1].x+b[0].x)));
    }
    return 0;
}

      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值