Largest Point

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1485    Accepted Submission(s): 588

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 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.

 
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
 
昨天输的很惨,中南地区邀请赛只A了两道题。所以感觉要多做少错思路题。
这个题分4种情况考虑,每种又分为三种情况。举一个例子:
a>0,b<0时
那么ti^2尽可能的大,tj尽可能的小。如果i!=j 那么直接取ti^2最大与tj最小。
如果i==j 那么在ti^2次大与tj最小和ti^2最大与tj次小中取大者。
还有就是 INF不能这样赋值 ,如1<<40 ,这个地方WA了好多次,,他会变成0
#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL INF = <<;
int main(){
int tcase;
scanf("%d",&tcase);
int t =;
while(tcase--){
int n,a,b;
scanf("%d%d%d",&n,&a,&b);
LL ans;
if(a>=&&b>=){
LL fmax = -INF,smax = -INF;
for(int i=;i<=n;i++){
LL num;
scanf("%lld",&num);
if(num>fmax){
smax = fmax;
fmax = num;
}else{
smax = max(smax,num);
}
}
ans = max(a*fmax*fmax+b*smax,a*smax*smax+b*fmax);
}else if(a>&&b<){
LL MAX=-INF,SMAX=-INF; ///平方的最大和次大
LL MIN=INF,SMIN=INF; ///最小和次小
int id1,id2;
for(int i=;i<=n;i++){
LL num;
scanf("%lld",&num);
LL temp = num*num;
if(num<MIN){
SMIN = MIN;
MIN = num;
id1 = i;
}else SMIN = min(SMIN,num);
if(temp>MAX){
SMAX = MAX;
MAX = temp;
id2 = i;
}else SMAX = max(SMAX,num);
}
if(id1!=id2){
ans = a*MAX+b*MIN;
}else{
ans = max(a*SMAX+b*MIN,a*MAX+b*SMIN);
}
}else if(a<&&b>){
LL MAX = -INF,MIN = INF; ///注意这里的最小是指平方的最小
LL smin=INF,smax = -INF; ///此处次小是指平方的次小
int id1,id2; ///分别记录ti 和 tj 的位置
for(int i=;i<=n;i++){
LL num;
scanf("%lld",&num);
if(num>MAX) {
smax = MAX;
MAX = num;
id1 = i;
}else smax = max(smax,num);
LL temp = num*num;
if(temp<MIN){
smin = MIN;
MIN = temp;
id2 = i;
}else smin = min(smin,temp);
}
if(id1!=id2){
ans = a*MIN+b*MAX;
}
else{
ans = max(a*MIN+b*smax,a*smin+b*MAX);
}
}else {
LL MIN = INF,SMIN = INF; ///这两个值代表绝对值次小和最小的平方
LL MIN1 = INF,SMIN1 = INF; ///这两个值代表次小和最小
int id1,id2;
for(int i=;i<=n;i++){
LL num;
scanf("%lld",&num);
LL temp = num*num;
if(num<MIN1) {
SMIN1 = MIN1;
MIN1 = num;
id1 = i;
}else SMIN1 = min(SMIN1,num); if(temp<MIN){
SMIN = MIN;
MIN = temp;
id2 = i;
}else SMIN = min(SMIN,temp);
}
if(id1!=id2){
ans = a*MIN+b*MIN1;
}else{
ans = max(a*MIN+b*SMIN1,a*SMIN+b*MIN1);
}
}
printf("Case #%d: %lld\n",t++,ans);
}
return ;
}

最新文章

  1. javascript 表单
  2. Server.Transfer 和 Response.Redirect 用法区别
  3. Javascript轮播 支持平滑和渐隐两种效果(可以只有两张图)
  4. 系统安全:Nessus Home版安装使用
  5. VS2008设置断点不命中
  6. debug note-- nginx php-fpm : Error:The page you are looking for is temporarily unavailable.
  7. 命名空间“Aspose”中不存在类型或命名空间名称“Slides”。
  8. 关于vue的使用计算属性VS使用计算方法的问题
  9. java+testng接口测试入门
  10. 10_bash_变量_条件判断及运算_sed_循环
  11. 【Linux常见问题】Centos7的网络配置问题
  12. commons-text StrBuilder字符串构建工具类例子
  13. HTTP 协议基础概念和报文结构
  14. 学习CSS布局 - position
  15. oracle登陆触发器及精细审计
  16. Maven中-DskipTests和-Dmaven.test.skip=true的区别
  17. 1.1使用java数组,并开始封装我们自己的数组
  18. TextSharp详情
  19. The way to Go(2): 语言的主要特性与发展的环境和影响因素
  20. RDLC_部署到不同的浏览器

热门文章

  1. 转 fine-tuning (微调)
  2. 【点分治】luoguP2664 树上游戏
  3. 【MySql】Mysql ERROR 1067: Invalid default value for ‘date’ 解决
  4. Python爬虫系列-Selenium+Chrome/PhantomJS爬取淘宝美食
  5. vue.js笔记1.0
  6. Html5_标签
  7. linux笔记(1)
  8. 【转】git bash here 右键菜单失效后的修复方法
  9. 各浏览器对 window.open() 的支持
  10. 细嚼慢咽 Mongoose 5