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 ;
}

hdu 5461(分类讨论)的更多相关文章

  1. HDU 5203 Rikka with wood sticks 分类讨论

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5203 bc(chinese):http://bestcoder.hdu.edu.cn/con ...

  2. HDU 6627 equation (分类讨论)

    2019 杭电多校 5 1004 题目链接:HDU 6627 比赛链接:2019 Multi-University Training Contest 5 Problem Description You ...

  3. HDU 6665 Calabash and Landlord (分类讨论)

    2019 杭电多校 8 1009 题目链接:HDU 6665 比赛链接:2019 Multi-University Training Contest 8 Problem Description Cal ...

  4. HDU5957 Query on a graph(拓扑找环,BFS序,线段树更新,分类讨论)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5957 题意:D(u,v)是节点u和节点v之间的距离,S(u,v)是一系列满足D(u,x)<=k的点 ...

  5. Codeforces 460D Little Victor and Set --分类讨论+构造

    题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四 ...

  6. BZOJ-1067 降雨量 线段树+分类讨论

    这道B题,刚的不行,各种碎点及其容易忽略,受不鸟了直接 1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2859 ...

  7. UVaLive 6862 Triples (数学+分类讨论)

    题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n. 析:是一个数 ...

  8. 枚举(分类讨论):BZOJ 1177: [Apio2009]Oil

    1177: [Apio2009]Oil Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 1477  Solved: 589[Submit] Descri ...

  9. hdu 5461 Largest Point

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

随机推荐

  1. Python爬虫系列-PyQuery详解

    强大又灵活的网页解析库.如果你觉得正则写起来太麻烦,如果你觉得BeautifulSoup语法太难记,如果你熟悉jQuery的语法,那么PyQuery就是你的最佳选择. 安装 pip3 install ...

  2. 【linux】【进程】stand alone 与 super daemon 区别

    本文引用自  鸟哥的linux私房菜如果依据 daemon 的启动与管理方式来区分,基本上,可以将 daemon 分为可独立启动的 stand alone , 与透过一支 super daemon 来 ...

  3. 【mysql】The server quit without updating PID file

      groupadd mysql useradd -r -g mysql mysql cd /usr/local/mysql chown -R mysql:mysql . scripts/mysql_ ...

  4. Python之路--序列化

    序列化的目的 1.以某种存储形式使自定义对象持久化 2.将对象从一个地方传递到另一个地方 3.使程序更具有维护性 json json多语言通用 四个功能:dumps.dump.loads.load # ...

  5. Python中__get__ ,__getattr__ ,__getattribute__用法与区别?

    class C(object): a = 'abc' def __getattribute__(self, *args, **kwargs): print("__getattribute__ ...

  6. TensorFlow L2正则化

    TensorFlow L2正则化 L2正则化在机器学习和深度学习非常常用,在TensorFlow中使用L2正则化非常方便,仅需将下面的运算结果加到损失函数后面即可 reg = tf.contrib.l ...

  7. Django中间件、Auth认证

    中间件 一:什么是中间件 是介于request与response处理之间的一道处理过程 二:中间件的作用 如果你想修改请求,例如被传送到view中的HttpRequest对象. 或者你想修改view返 ...

  8. [adb 学习篇] adb pull

    adb pull   E:\uitest\testcase\CaseDemo\testcase\3dmark\3DMarkAndroid   /sdcard/3DMarkAndroid 假设:  E: ...

  9. [linux小技巧]批量移动文件

    for i in {1..23};do mv test$i/ ../;done

  10. Model View Controller(MVC) in PHP

    The model view controller pattern is the most used pattern for today’s world web applications. It ha ...