hdu 5461 Largest Point

最大化序列值算法
本文介绍了一种算法,用于从整数序列中选择两个元素,并通过特定公式计算最大化的值。该算法根据不同系数的正负性分四类处理,确保了在不同情况下都能找到最优解。
Given the sequence A A with n n integers t 1 ,t 2 ,,t n  t1,t2,⋯,tn. Given the integral coefficients a a and b b. The fact that select two elements t i  ti and t j  tj of A A and ij i≠j to maximize the value of at 2 i +bt j  ati2+btj, becomes the largest point.
Input
An positive integer T T, indicating there are T T test cases.
For each test case, the first line contains three integers corresponding to n (2n5×10 6 ), a (0|a|10 6 ) n (2≤n≤5×106), a (0≤|a|≤106) and b (0|b|10 6 ) b (0≤|b|≤106). The second line contains n n integers t 1 ,t 2 ,,t n  t1,t2,⋯,tn where 0|t i |10 6  0≤|ti|≤106 for 1in 1≤i≤n.

The sum of n n for all cases would not be larger than 5×10 6  5×106.
Output
The output contains exactly T T lines.
For each test case, you should output the maximum value of at 2 i +bt j  ati2+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

按照ab的符号分好四种情况就可以了

ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long long a1[5000005];
int main()
{
    long long n,a,b,t;
    cin>>t;
    int p=0;
    while(t--)
    {
       p++;
       cin>>n>>a>>b;
       cin>>a1[0];
       long long max1=abs(a1[0]),min1=abs(a1[0]),max2=a1[0],min2=a1[0];
       if(a<=0&&b<=0)
       {
           int k=0;
           for(int i=1;i<n;i++)
           {
               scanf("%lld",&a1[i]);
               if(abs(a1[i])<min1)
               {min1=abs(a1[i]);k=i;}
           }
           int flag=0;
           for(int i=1;i<n;i++)
           {
               if(a1[i]<min2&&i!=k)
               {
                   min2=a1[i];
               }
           }
           printf("Case #%d: %lld\n",p,a*min1*min1+b*min2);
       }
       if(a<=0&&b>0)
       {
           int k=0;
          for(int i=1;i<n;i++)
           {
               scanf("%lld",&a1[i]);
               if(abs(a1[i])<min1)
               {min1=abs(a1[i]);k=i;}
           }
           for(int i=1;i<n;i++)
           if(a1[i]>max2&&i!=k)
           max2=a1[i];
           printf("Case #%d: %lld\n",p,a*min1*min1+b*max2);
       }
       if(a>0&&b<=0)
       {
           int k=0;
           for(int i=1;i<n;i++)
           {
               scanf("%lld",&a1[i]);
               if(abs(a1[i])>max1)
               {max1=abs(a1[i]);k=i;}
           }
           for(int i=1;i<n;i++)
           {
               if(a1[i]<min2&&i!=k)
               min2=a1[i];
           }
           printf("Case #%d: %lld\n",p,a*max1*max1+b*min2);
       }
       if(a>0&&b>0)
       {
           int k=0;
           for(int i=1;i<n;i++)
           {
               scanf("%lld",&a1[i]);
               if(abs(a1[i])>max1)
               {max1=abs(a1[i]);k=i;}
           }
           for(int i=1;i<n;i++)
           {
               if(a1[i]>max2&&i!=k)
               max2=a1[i];
           }
           printf("Case #%d: %lld\n",p,a*max1*max1+b*max2);
       }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值