HDU 5461 Largest Point

本文介绍了一个算法问题,即从给定序列中选择两个不同的元素,通过特定公式计算最大值。文章提供了一段AC代码实现,该算法首先对元素进行排序,并巧妙地处理了选取元素时的特殊情况。
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 ij 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 (2n5×106), a (0|a|106) and b (0|b|106). The second line contains n integers t1,t2,,tn where 0|ti|106 for 1in.

The sum of
n for all cases would not be larger than 5×106.
 

Output
The output contains exactlyT 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
 注意题目中i!=j 的条件

AC代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
struct number
{
    long long shu,xulie;
}x[500005],y[500005];
int cmp(number k,number l)
{
    return k.shu<l.shu;
}
int main()
{
    long long c,t,n,a,b,i,temp;
    scanf("%lld",&t);
    c=0;
    while(t--)
    {
        scanf("%lld%lld%lld",&n,&a,&b);
        for(i=0;i<n;i++)
        {
            scanf("%lld",&temp);
            x[i].shu=a*temp*temp;
            x[i].xulie=i;
            y[i].shu=b*temp;
            y[i].xulie=i;
        }
        sort(x,x+n,cmp);
        sort(y,y+n,cmp);
        printf("Case #%lld: %lld\n",++c,(x[n-1].xulie==y[n-1].xulie)?(max(x[n-1].shu+y[n-2].shu,x[n-2].shu+y[n-1].shu)):(x[n-1].shu+y[n-1].shu));
    }
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值