您的位置:首页 > 其它

HDU-5461 Largest Point(沈阳网赛,最值)

2015-09-22 13:06 267 查看


Largest Point

Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 825 Accepted Submission(s): 338



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 i≠j 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 (2≤n≤5×106), a (0≤|a|≤106) and b (0≤|b|≤106).
The second line contains nintegers 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.



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




Source

2015 ACM/ICPC Asia Regional Shenyang Online


题意:给出n,a,b和 t1到tn,n个数的数列。求i !=j 使得a*ti*ti+b*tj最大。输出最大值。

思路:可以看到最大值只与数列的最大、次大、最小、次小和绝对值的最小,次小有关,输入时记录下这六个数,暴力解出就好了,注意i!=j 和 数据范围。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;

#define LL long long 

const LL MAXN = 5000000 + 10;
const LL INF = 1e18 + 1000;
LL num[MAXN];

struct node
{
	LL num;
	LL id;
};

int main()
{
	LL casen;
	LL n, a, b;
	scanf("%I64d", &casen);
	for (LL cas = 1; cas <= casen; cas++)
	{
		scanf("%I64d%I64d%I64d", &n, &a, &b);

		
		node max1, max2, min1, min2, minabs1, minabs2;
		max1.num = -INF, max2.num = -INF, min1.num = INF, min2.num = INF, minabs1.num = INF, minabs2.num = INF;

		for (LL i = 0; i < n; i++)
		{
			scanf("%I64d", &num[i]);
			if (num[i]>max1.num)
			{
				max1.num = num[i];
				max1.id = i;
			}
			if (num[i] < min1.num)
			{
				min1.num = num[i];
				min1.id = i;
			}
			if (abs(num[i]) < abs(minabs1.num))
			{
				minabs1.num = num[i];
				minabs1.id = i;
			}
		}

		for (LL i = 0; i < n; i++)
		{
			if (num[i]>max2.num && i!=max1.id)
			{
				max2.num = num[i];
				max2.id = i;
			}
			if (num[i] < min2.num && i!=min1.id)
			{
				min2.num = num[i];
				min2.id = i;
			}
			if (abs(num[i]) < abs(minabs2.num) && i!=minabs1.id)
			{
				minabs2.num = num[i];
				minabs2.id = i;
			}
		}

		LL res[6];
		res[0] = max1.id;
		res[1] = max2.id;
		res[2] = min1.id;
		res[3] = min2.id;
		res[4] = minabs1.id;
		res[5] = minabs2.id;

		LL ans = -INF;

		for (LL i = 0; i < 6; i++)
		for (LL j = 0; j < 6; j++)
		{
			if (res[i] != res[j])
			{
				LL temp = a*num[res[i]] * num[res[i]] + b*num[res[j]];
				ans = max(ans, temp);
			}
		}

		printf("Case #%I64d: %I64d\n", cas, ans);
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: