Largest Point

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

Total Submission(s): 1102    Accepted Submission(s): 429

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

题意给了a b的两个值,然后给了一大堆t的值,问在这些t的值里面求最大的 at2i+btj.

分情况讨论,对于a大于零小于零,b大于零小于零。然后记录最大值 次大值 最小值 次小值 还有一个绝对值最小值,这五个值就够用了。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; long long test, n, a, b;
long long i, j;
long long abs_min, max1, max2, min1, min2; int main()
{ long long temp;
cin >> test;
for (i = 1; i <= test; i++)
{
abs_min = 1000005;
max1 = -1000005;
max2 = -1000005;
min1 = 1000005;
min2 = 1000005; cin >> n >> a >> b; for (j = 1; j <= n; j++)
{
scanf("%lld",&temp); if (abs(temp) < abs_min)
{
abs_min = abs(temp);
}
if (temp > max1)
{
max2 = max1;
max1 = temp;
}
else if (temp > max2)
{
max2 = temp;
} if (temp < min1)
{
min2 = min1;
min1 = temp;
}
else if (temp < min2)
{
min2 = temp;
}
}
long long res;
if (a >= 0 && b>=0)
{
res = a*max1* max1 + b*max2;
res = max(res, a*min1 * min1 + b*max1);
}
else if (a >= 0 && b <= 0)
{
res = a*max1 * max1 + b*min1;
res = max(res, a*min1 * min1 + b*min2);
}
else if (a <= 0 && b <= 0)
{
if (min1 == abs_min)
{
res = a*abs_min*abs_min + b*min2;
}
else
{
res = a*abs_min*abs_min + b*min1;
}
}
else
{
if (max1 == abs_min)
{
res = a*abs_min*abs_min + b*max2;
}
else
{
res = a*abs_min*abs_min + b*max1;
}
}
cout << "Case #"<<i<<": "<<res<< endl;
} return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5461:Largest Point的更多相关文章

  1. LeetCode之“排序”:Largest Number

    题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...

  2. HDU - 6409:没有兄弟的舞会(数学+思维)

    链接:HDU - 6409:没有兄弟的舞会 题意: 题解: 求出最大的 l[i] 的最大值 L 和 r[i] 的最大值 R,那么 h 一定在 [L, R] 中.枚举每一个最大值,那么每一个区间的对于答 ...

  3. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  4. hdu 5461 Largest Point 暴力

    Largest Point Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  5. hdu 5461(2015沈阳网赛 简单暴力) Largest Point

    题目;http://acm.hdu.edu.cn/showproblem.php?pid=5461 题意就是在数组中找出a*t[i]*t[i]+b*t[j]的最大值,特别注意的是这里i和i不能相等,想 ...

  6. hdu 5461 Largest Point

    Thinking about it: 对于式子 a * ti * ti + b * tj,可以看作时有两部分构成 a * ti * ti 和 b * tj,如果整个式子要最大,则要求这两部分都要尽量大 ...

  7. HDU 5510:Bazinga(暴力KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5510 Bazinga Problem Description   Ladies and gentlemen, p ...

  8. HDU 2732:Leapin' Lizards(最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意:给出两个地图,蜥蜴从一个柱子跳跃到另外一个地方,那么这个柱子就可能会坍塌,第一个地图是柱子可以容忍跳 ...

  9. HDU 4289:Control(最小割)

    http://acm.hdu.edu.cn/showproblem.php?pid=4289 题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可 ...

随机推荐

  1. flutter 启动时一直Resolving dependencies...

    原因:国内网无法从Google获取资源,貌似搭了梯子也没用 修改flutter sdk Path/packages/flutter_tools/gradle/flutter.gradle这个文件,使用 ...

  2. P1250 种树 题解

    题目描述 一条街道的一边有几座房子,因为环保原因居民想要在路边种些树,路边的居民被分割成 n 块,并被编号为 1…n.每块大小为一个单位尺寸并最多可种一棵树.每个居民想在门前种些树并指定了三个数b,e ...

  3. (转)Dom4j中的中文编码问题

    一.“中文问题没商量”之Dom4j中的编码问题  本文主要讲述的是Dom4j在把Document保存到文件过程中出现的一个中文问题,本文跟<80前>一文一样,以Spring项目无关,请“春 ...

  4. Unity ShaderLab 学习笔记(一)

    因为项目的问题,有个效果在iOS上面无法实现出来- 因为shader用的HardSurface的,在android上面跑起来没有问题- 以为在iOS上也不会有问题,但是悲剧啊,技能效果一片漆黑- 而且 ...

  5. jmeter之Xpath提取器

    首先创建线程组,添加http请求,具体的设置如图1所示: 图1 然后,再添加后置处理器中的XPath Extractor,具体的参数设置,以及表达式如图2: 图2 可以添加Debug PostProc ...

  6. flutter如何使用配置文件pubspec.yaml(位于项目根目录)来管理第三方依赖包

    官方文档 在软件开发中,很多时候有一些公共的库或SDK可能会被很多项目用到,因此,将这些代码单独抽到一个独立模块,然后哪个项目需要使用时再直接集成这个模块,便可大大提高开发效率.很多编程语言或开发工具 ...

  7. Java8 使用LocalDate计算两个日期间隔多少年,多少月,多少天

    最近项目遇到一个需要计算两个日期间隔的期限,需要计算出,整年整月整日这样符合日常习惯的说法,利用之前的Date和Calendar类会有点复杂,刚好项目使用了JDK8,那就利用起来这个新特性,上代码: ...

  8. iOS大V博客

    王巍的博客:王巍目前在日本横滨任职于LINE.工作内容主要进行Unity3D开发,8小时之外经常进行iOS/Mac开发.他的陈列柜中已有多款应用,其中番茄工作法工具非常棒. http://onevca ...

  9. Minikube安装

    参考 https://blog.csdn.net/liumiaocn/article/details/52041726?locationNum=4&fps=1 中文社区API http://d ...

  10. JAVA笔记01 变量的取名

    第2章 有意义的命名2.1 介绍2.2 名副其实 变量名太随意,haha.list1.ok 这些都没啥意义2.3 避免误导 包含List等关键字.字母o与数字0等2.4 做有意义的区分 反面教材,变量 ...