hdoj 5461 Largest Point 【枚举所有情况 找最大值】



Largest Point

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 379    Accepted Submission(s): 170


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 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
 



题意:给你一个n个数组成的序列,又给出两个数a和b,让你从序列中找出两个元素t1和t2使得a * t1 * t1 + b * t2值最大。输出最大值。


分析:t1 和 t2只能取序列的 最大绝对值、最小绝对值、最大值、最小值中的两个。


思路:枚举a和b的不同情况,分别求解最大值。 



AC代码:


#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define MAXN 5000000+10
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
LL c[MAXN];
LL a, b;
LL f(LL x, LL y)//a和b的系数
{
    return a * x * x + b * y;
}
int main()
{
    int t, k = 1;
    int n;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%lld%lld", &n, &a, &b);
        LL Minabs = INF;//绝对值最小的数
        for(int i = 0; i < n; i++)
            scanf("%lld", &c[i]), Minabs = min(Minabs, abs(c[i]));
        printf("Case #%d: ", k++);
        if(n == 2)
        {
            printf("%lld\n", max(f(c[0], c[1]), f(c[1], c[0])));
            continue;
        }
        sort(c, c+n);//升序排列
        if(a == 0)
        {
            if(b == 0)
                printf("0\n");
            else if(b > 0)
                printf("%lld\n", f(0, c[n-1]));
            else
                printf("%lld\n", f(0, c[0]));
        }
        else if(a > 0)
        {
            if(b == 0)
                printf("%lld\n", f(max(abs(c[0]), abs(c[n-1])), 0));//乘以绝对值最大的数
            else if(b > 0)
            {
                LL x1 = f(max(abs(c[0]), abs(c[n-2])), c[n-1]);//b系数有两个可能
                LL x2 = f(max(abs(c[0]), abs(c[n-1])), c[n-2]);
                printf("%lld\n", max(x1, x2));
            }
            else
            {
                LL x1 = f(max(abs(c[1]), abs(c[n-1])), c[0]);//b系数有两种可能
                LL x2 = f(max(abs(c[0]), abs(c[n-1])), c[1]);
                printf("%lld\n", max(x1, x2));
            }
        }
        else//a和b均小于0
        {
            if(b == 0)
                printf("%lld\n", f(Minabs, 0));//乘以绝对值最小的数
            else if(b > 0)
            {
                if(c[n-1] == Minabs)//全是非正数
                    printf("%lld\n", max(f(c[n-1], c[n-2]), f(c[n-2], c[n-1])));
                else//存在正数
                    printf("%lld\n", f(Minabs, c[n-1]));
            }
            else
            {
                if(c[0] == Minabs)//全是非负数
                    printf("%lld\n", max(f(c[1], c[0]), f(c[0], c[1])));
                else//存在负数
                    printf("%lld\n", f(Minabs, c[0]));
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值