upc 8262: LARGEST POINT

题目描述

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  becomes the largest point.

 

输入

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 .

 

输出

The output contains exactly T lines. For each test case, you should output the maximum value of 

 

样例输入

2
3 2 1
1 2 3
5 -1 0
-3 -3 0 3 3

 

样例输出

Case #1: 20
Case #2: 0

题意:计算a*ti*ti+b*tj的最大值,且j != i

思路:分别计算a*t*t的值和b*t的值,并记录t的下标,比较两者最大值下标是否相等,不等直接输出结果,相等再比较次

最大值,输出结果。。。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef struct node
{
    ll step;//记录下标
    ll value;//记录值
}p;
p maxx[1000010],mbxx[1000010];//分别存a*t*t和b*t的值和下标
bool cmp(p a,p b)
{
    return a.value>b.value;
}
int main()
{
    int t;
    int temp=1;
    scanf("%d",&t);
    while(t--)
    {
        ll n,a,b;
        scanf("%lld%lld%lld",&n,&a,&b);
        for(int i=0;i<n;i++)
        {
            ll x;
            scanf("%lld",&x);
            maxx[i].step=i;
            maxx[i].value=a*x*x;
            mbxx[i].step=i;
            mbxx[i].value=b*x;
        }
        sort(maxx,maxx+n,cmp);
        sort(mbxx,mbxx+n,cmp);
        if(maxx[0].step!=mbxx[0].step)//判断最大值下标
            printf("Case #%d: %lld\n",temp++,maxx[0].value+mbxx[0].value);
        else
            printf("Case #%d: %lld\n",temp++,maxx[0].value+mbxx[1].value>maxx[1].value+mbxx[0].value?maxx[0].value+mbxx[1].value:maxx[1].value+mbxx[0].value);
        

    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值