码迷,mamicode.com
首页 > 其他好文 > 详细

Largest Point

时间:2015-09-19 19:34:39      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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 nintegers 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
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int oo = 1e9;
const int N = 5*1e6+8;
const int M = 6000;
typedef long long LL;
int ac[N], n;
LL ans;
struct da
{
    int num, id;
} as[N];
int cmp1(da a, da b)
{
    return a.num < b.num;
}
int cmp2(da a, da b)
{
    return abs(a.num) < abs(b.num);
}
int main()
{
    int i, T, a, b, A1, A2, B1, B2, ma, mb, xx=1;
    LL sum;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d %d", &n, &a, &b);
        for(i = 0; i < n; i++)
        {
            scanf("%d", &as[i].num);
            as[i].id = i;
        }
        A1 = A2 = B1 = B2 = 0;
        sort(as, as+n, cmp2);
        if(a >= 0) A1 = as[n-1].num, A2 = as[n-1].id, ma = as[n-2].num;
        else A1 = as[0].num, A2 = as[0].id, ma = as[1].num;
        sort(as, as+n, cmp1);
        if(b >= 0) B1 = as[n-1].num, B2 = as[n-1].id, mb = as[n-2].num;
        else B1 = as[0].num, B2 = as[0].id, mb = as[1].num;
        if(A2 != B2)
        {
            ans = (LL)a*A1*A1;
            ans += (LL)b*B1;
        }
        else
        {
            ans = (LL)a*A1*A1;
            ans += (LL)b*mb;
            sum = (LL)a*ma*ma;
            sum += (LL)b*B1;
            ans = max(ans, sum);
        }
        printf("Case #%d: %I64d\n", xx++, ans);
    }
    return 0;
}

  

Largest Point

标签:

原文地址:http://www.cnblogs.com/PersistFaith/p/4821904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!