hdu 5461 Largest Point 暴力
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暴力枚举= =
@)1%KBO0HM418$J94$1R.jpg)
代码:
#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 暴力的更多相关文章
- hdu 5461 Largest Point
Thinking about it: 对于式子 a * ti * ti + b * tj,可以看作时有两部分构成 a * ti * ti 和 b * tj,如果整个式子要最大,则要求这两部分都要尽量大 ...
- hdu 5461(2015沈阳网赛 简单暴力) Largest Point
题目;http://acm.hdu.edu.cn/showproblem.php?pid=5461 题意就是在数组中找出a*t[i]*t[i]+b*t[j]的最大值,特别注意的是这里i和i不能相等,想 ...
- HDU 5461:Largest Point
Largest Point Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) To ...
- hdoj 5461 Largest Point
Largest Point Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)
E - Largest Rectangle in a Histogram Time Limit:1000MS Memory Limit:32768KB 64bit IO Format: ...
- HDU 1506 Largest Rectangle in a Histogram set+二分
Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...
- hdu 5762 Teacher Bo 暴力
Teacher Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...
- HDU 1333 基础数论 暴力
定义一种数位simth数,该数的各位之和等于其所有质因子所有位数字之和,现给出n求大于n的最小该种数,n最大不超过8位,那么直接暴力就可以了. /** @Date : 2017-09-08 14:12 ...
- HDU 4618 Palindrome Sub-Array 暴力
Palindrome Sub-Array 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4618 Description A palindrome s ...
随机推荐
- python中的commands模块,执行出错:'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
最近发现了python的commands模块,查看了下源码,使用的popen封装的,形成三个函数getstatus(), getoutput(), getstatusoutput() 源码如下: de ...
- WindowsServer2008配置MySql Proxy
WIndowsServer2008配置MySql Proxy: 1.MySql Proxy的下载地址:http://dev.mysql.com/downloads/mysql-proxy/,选择Win ...
- Unity3D如何获取对象和子对象
在Unity3d中获取游戏对象有三种方法: 一:获取对象 1.通过对象名称获取:objCube=GameObject.Find("Cube"); private var objCu ...
- UVA 1515 Pool construction 水塘(最大流,经典)
题意: 给一个h*w的矩阵,每个格子中是'#'和'.'两个符号之一,分别代表草和洞.现在要将洞给围起来(将草和洞分离),每条边需花费b元(即将一个洞包起来需要4边,将2个连续的洞包起来需要6边,省了2 ...
- 剑指Offer:打印从1到最大的n位数
题目:输入数值n,按顺序打印从1到最大的n位数,例如输入n=3,则从1,2,3,一直打印到999 陷阱:若使用循环遍历 1- 999...9 并依次输出,当位数n过大时,无论将其存入int或long或 ...
- Android学习的一些问题
如何让Service常驻后台? 如何让App自启动? 如何让App自动更新? Handler Adapter Bundle Application getXXX()
- 用JS动态创建登录表单,报了个小错误
后来发现原来是: dvObj.style.border='#Red 1px sold'; 其中的Red多谢了一个‘#’, 但是奇怪的是在chrome和firefox都备有报错,但是在ie中报错了. 各 ...
- Go语言项目的错误和异常管理 via 达达
Go语言项目的错误和异常管理 最近连续遇到朋友问我项目里错误和异常管理的事情,之前也多次跟团队强调过错误和异常管理的一些概念,所以趁今天有动力就赶紧写一篇Go语言项目错误和异常管理的经验分享. 首先我 ...
- 从ramdisk根文件系统启动Linux 二
今天做了个试验,让Linux2.6.29.4从ramdisk根文件系统启动成功,总结一下.其中涉及的内容较多,很多东西不再详述,如需深入研究请查阅相关资料(百度或谷歌一下一大堆). 开发环境:Fedo ...
- 每天一个linux 命令:which
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. ...