Largest Point

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5461

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

HINT

题意

给你a,b,再给你n个数

然后让你求ati*ti+btj最大值是多少

题解:

我们是暴力做的,找到最大的两个数,最小的两个数,绝对值最大的两个数,绝对值最小的两个数

然后扔进一个vector里面,然后去重,然后暴力枚举的

但是还是怕tle,就分治了一下解法,数据小的话就直接n^2暴力枚举= =

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1) using namespace std;
const int maxn = 5e6 + ;
long long a , b , ans , p[maxn];
int vis[] , used[maxn] , n , sz;
vector<long long>s; struct data
{
long long val;
int idx;
}; data A[maxn] , B[maxn]; bool cmp1(const data & x,const data & y)
{
return x.val < y.val;
} bool cmp2(const data & x,const data & y)
{
return abs(x.val) < abs(y.val);
} void initiation()
{
scanf("%d%I64d%I64d",&n,&a,&b);
memset(used,,sizeof(int)*(n+));
for(int i = ; i < n ; ++ i)
{
scanf("%I64d",&A[i].val);
A[i].idx = i;
B[i].val = A[i].val;
B[i].idx = i;
p[i] = A[i].val;
}
s.clear();
ans = -(1LL<<);
} void dfs(int cur , long long check)
{
if(cur == ) ans = max( ans , check);
else
{
for(int i = ; i < sz ; ++ i)
{
if(!vis[i])
{
vis[i] = ;
if(cur == ) dfs(cur + , check + a * s[i]*s[i] );
else dfs(cur + , check + b*s[i]);
vis[i] = ;
}
}
}
} long long solve()
{
sort(A,A+n,cmp1);
sort(B,B+n,cmp2);
used[A[].idx] = , used[A[].idx] = , used[A[n-].idx] = , used[A[n-].idx] = ;
used[B[].idx] = , used[B[].idx] = ; used[B[n-].idx] = , used[B[n-].idx] = ;
for(int i = ; i < n ; ++ i) if(used[i]) s.push_back(p[i]);
sz = s.size();
memset(vis,,sizeof(vis));
dfs(,);
return ans;
} long long solve2()
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if(i==j) continue;
ans = max(ans,A[i].val*A[i].val*a+b*A[j].val);
}
}
return ans;
} int main(int argc,char *argv[])
{
int Case;
scanf("%d",&Case);
for(int cas = ; cas <= Case ; ++ cas)
{
initiation();
printf("Case #%d: ",cas);
if(n<=) printf("%I64d\n",solve2());
else printf("%I64d\n",solve());
}
return ;
}

hdu 5461 Largest Point 暴力的更多相关文章

  1. hdu 5461 Largest Point

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

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

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

  3. HDU 5461:Largest Point

    Largest Point Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  4. hdoj 5461 Largest Point

    Largest Point Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  5. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  6. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

  7. hdu 5762 Teacher Bo 暴力

    Teacher Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...

  8. HDU 1333 基础数论 暴力

    定义一种数位simth数,该数的各位之和等于其所有质因子所有位数字之和,现给出n求大于n的最小该种数,n最大不超过8位,那么直接暴力就可以了. /** @Date : 2017-09-08 14:12 ...

  9. HDU 4618 Palindrome Sub-Array 暴力

    Palindrome Sub-Array 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4618 Description A palindrome s ...

随机推荐

  1. Android 之 内存管理-查看内存泄露(三)

    概述 在android的开发中,要时刻主要内存的分配和垃圾回收,因为系统为每一个dalvik虚拟机分配的内存是有限的,在google的G1中,分配的最大堆大小只有16M,后来的机器一般都为24M,实在 ...

  2. hibernate实体的几种状态:

    hibernate实体的几种状态: 实体的生命周期中,实体主要经过瞬时(Transient),托管(Attatched或Managed),游离(Detached)和销毁(Removed)四个状态. 瞬 ...

  3. 【MySql存储过程】DATE_ADD用法

    定义和用法 DATE_ADD() 函数向日期添加指定的时间间隔. 语法 DATE_ADD(date,INTERVAL expr type) date 参数是合法的日期表达式.expr 参数是您希望添加 ...

  4. Hibernate4.x之映射关系--双向1-n

    双向1-n与双向n-1是完全相同的两种情形 双向1-n需要在1的一端可以访问n的一端,反之亦然. 域模型:从Order到Customer的多对一双向关联需要在Order类中定义一个Customer属性 ...

  5. virtualbox中ubuntu和windows共享文件夹设置

    系统平台:win8.1.virtualbox4.3.8.ubuntu12.041.安装VBoxGuestAdditions_4.3.8.iso增强工具,安装完毕后根据提示重启Ubuntu,具体操作如下 ...

  6. MySQL table_id原理及风险分析

    1. 什么是table_id MySQL binlog文件按格式分为文件头部和事件信息.文件头部占4字节,内容固定为:"\xfe\x62\x69\x6e",接下来就是各个event ...

  7. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.7

    The set of all invertible matrices is a dense open subset of the set of all $n\times n$ matrices. Th ...

  8. 0、IOS8:Xcode6 playground

    一.Playground介绍 Playground是Xcode6中自带的Swift代码开发环境.俗话说“功欲善其事,必先利其器”.以前在Xcode5中编写脚本代码,例如编写JS,其编写过程很痛苦,Xc ...

  9. 那些年一起踩过的坑 — Date类型序列化的问题

      坑在哪里?        序列化 和 反序列化 的时候对Date字段的格式设置不一致        例如:将Java bean序列化成Json string的时候 格式为 yyyy-MM-dd 解 ...

  10. c#: 解析json, 转成xml, 简单方便

    没看到.net framework中有这样的功能, 懒得到处找了, 索性花点时间自己写一个 /* * Created by SharpDevelop. * Date: 2013/6/24 * User ...