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); } }
相关文章推荐
- 提升SQLite数据插入效率低、速度慢的方法
- 【计算机网络】当输入URL后发生了什么
- 栈的压入、弹出序列
- 操作系统---------显示模式切换
- Centos重启网卡失败原因
- .stl文件(CAD三维模型)格式 及 基于C/C++的数据读取
- sed命令——流编辑器
- 数据库连接超时
- C# WinForm程序中使用Unity3D控件
- 访问者模式(5.11)之对象结构
- (算法)旋转有序数组中查找某个数
- VC6.0的定时器设计
- 调试
- JMS Topic 和queue 的区别
- 基于AVR128单纯Modbus协议实施
- 侧拉菜单
- SAP FI 财务模块 关键用户 考试练习 问卷
- C中的getline实现
- C# 中 textBox 侧面滑条 属性
- 使用RadioButton 和RadioGroup