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. Linux - NodeJS安装

    1> 去NodeJS官网 https://nodejs.org/en/ 或 中文网 http://nodejs.cn/download/ 拷贝相应版本的安装文件,如下图: 2> 执行 wg ...

  2. 11Vim文本编辑器

    Vim文本编辑器 在Linux系统中一切都是文件,而配置一个服务就是在修改其配置文件的参数. Vim提供了三种模式:命令模式.输入模式.末行模式 1.命令模式 每次运行Vim编辑器时,默认进入命令模式 ...

  3. python入门:模拟简单用户登录(自写)

    #!/usr/bin/env python # -*- coding: utf-8 -*- #模拟简单用户登录(自写) import getpass a = raw_input("Pleas ...

  4. Kubernetes的主要功能

    Kubernetes的主要功能 1.数据卷   Pod中容器之间共享数据,可以使用数据卷. 2.应用程序健康检查   容器内服务可能进程堵塞无法处理请求,可以设置监控检查策略保证应用健壮性. 3.复制 ...

  5. 原来针对新唐mcu,keil有免费许可

    MDK for Nuvoton Cortex-M0/M23:The MDK for Nuvoton Cortex-M0/M23 is a license paid by Nuvoton. It is  ...

  6. Leetcode 559. N叉树的最大深度

    题目链接 https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/description/ 题目描述 给定一个N叉树,找到其最大深度. ...

  7. nuc 第二届山西省大学生程序设计大赛 魔力手环

    problem 很妙啊--发现状态转移矩阵每一行都可以由上一行平移得到,每次只算第一行然后平移,\(O(n^3)\) 就变成了 \(O(n^2)\). #include <iostream> ...

  8. [转载]ExtJs4 笔记(1) ExtJs大比拼JQuery:Dom文档操作

    出处:[Lipan] (http://www.cnblogs.com/lipan/) 现在主流的JS框架要数ExtJs和JQuery应用的比较广泛.JQuery属于轻量级的,一般做网站应用比较常见,可 ...

  9. 大数据学习——spark-steaming学习

    官网http://spark.apache.org/docs/latest/streaming-programming-guide.html 1.1.  用Spark Streaming实现实时Wor ...

  10. Python面试题(练习一)

    1.Python的可变类型和不可变类型? 可变类型:list.dict(列表和字典) 不可变类型:数字.字符串.元组 2.求结果: v = dict.fromkeys(['k1','k2'],[]) ...