1. 程式人生 > >upc 8262: LARGEST POINT

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