hdu 6435 CSGO(最大曼哈顿距离)
There are n Main Weapons and m Secondary Weapons in CSGO. You can only choose one Main Weapon and one Secondary Weapon. For each weapon, it has a composite score S.
The higher the composite score of the weapon is, the better for you.
Also each weapon has K performance evaluations x[1], x[2], …, x[K].(range, firing rate, recoil, weight…)
So you shold consider the cooperation of your weapons, you want two weapons that have big difference in each performance, for example, AWP + CZ75 is a good choose, and so do AK47 + Desert Eagle.
All in all, you will evaluate your weapons by this formula.(MW for Main Weapon and SW for Secondary Weapon)
$$$$$$S_{MW}+S_{SW}+\sum_{i=1}^{k}{|x_{MW}[i]-x_{SW}[i]|}$$$$$$
Now you have to choose your best Main Weapon & Secondary Weapon and output the maximum evaluation.
On the first line, there is a positive integer T, which describe the number of data. Next there are T groups of data.
for each group, the first line have three positive integers n, m, K.
then, the next n line will describe n Main Weapons, K+1 integers each line S, x[1], x[2], …, x[K]
then, the next m line will describe m Secondary Weapons, K+1 integers each line S, x[1], x[2], …, x[K]
There is a blank line before each groups of data.
T<=100, n<=100000, m<=100000, K<=5, 0<=S<=1e9, |x[i]|<=1e9, sum of (n+m)<=300000
2 2 1
0 233
0 666
0 123
0 456
2 2 1
100 0 1000 100 1000 100
100 0
2000
上面的代数式,看似最合理也最基本的做法,就是遍历$$$i,j$$$,计算所有向量之间的曼哈顿距离,复杂度为$$$O(C_n^2\times m)$$$,当$$$n$$$的值很大时,暴力将会超时,所以要从很小的m入手,采用一种直觉上很反智(仅个人观点),但是对计算机很高效的算法。
把绝对值打开会得到两种情况,比如$$$|x_i-x_j|$$$要么是$$$(x_i-x_j)$$$,要么是$$$(-x_i+x_j)$$$,$$$x_i$$$前面的系数是1/-1对应它较大/较小的情况;如果放在五维上分析的话,把两个五维向量$$$A: (a_1,a_2,a_3,a_4,a_5), B: (b_1,b_2,b_3,b_4,b_5)$$$的曼哈顿距离的代数式打开,将有$$$2^5=32$$$种情况,每种情况中,$$$a_i$$$的系数可以是$$$1$$$或$$$-1$$$,分别对应$$$a_i$$$在此情况下是较大还是较小,$$$b_i$$$也是同理,而且$$$b_i$$$的系数一定和$$$a_i$$$的系数相反。如果把$$$A$$$和$$$B$$$的系数单独提取出来,变成下面这样
$$$$$$
\begin{align}
& \sum_{i=1}^{5}{(t_{a_i}a_i-t_{a_i}b_i)}\\
=& (a_1,a_2,a_3,a_4,a_5)\times
\begin{pmatrix}
t_{a_1}\\
t_{a_2}\\
t_{a_3}\\
t_{a_4}\\
t_{a_5}
\end{pmatrix}
+(b_1,b_2,b_3,b_4,b_5)\times
\begin{pmatrix}
-t_{a_1}\\
-t_{a_2}\\
-t_{a_3}\\
-t_{a_4}\\
-t_{a_5}
\end{pmatrix}\\[3ex]
=& (a_1,a_2,a_3,a_4,a_5)\times
\begin{pmatrix}
t_{a_1}\\
t_{a_2}\\
t_{a_3}\\
t_{a_4}\\
t_{a_5}
\end{pmatrix}
-(b_1,b_2,b_3,b_4,b_5)\times
\begin{pmatrix}
t_{a_1}\\
t_{a_2}\\
t_{a_3}\\
t_{a_4}\\
t_{a_5}
\end{pmatrix}
\end{align}
$$$$$$
换一个角度看问题,序列$$$ \begin{pmatrix}
t_{a_1}\\
t_{a_2}\\
t_{a_3}\\
t_{a_4}\\
t_{a_5}
\end{pmatrix}$$$,其实相当于是去衡量$$$A$$$和$$$B$$$大小的一个标准,$$$A$$$和$$$B$$$经过序列的处理,直接就可以比较大小了,并求出差值。那么用它也同样可以用来衡量所有的向量。有了衡量标准的好处是,我们就不用再把向量两两比较才能知道最大的差了,只需要用这个标准来计算出所有向量的值(也就是把每个向量乘上这个序列),用最大值减去最小值就得到了最大的差。
虽然我们并不知道,选用哪个$$$ \begin{pmatrix}
t_1\\
t_2\\
t_3\\
t_4\\
t_5
\end{pmatrix}$$$才是真正的最大差,才是原代数式的值,但是所有的序列包含了所有可能的情况,一定有一个序列对应着原代数式的值,只要遍历了所有的序列,最大的那个就是答案。再看一下复杂度,对于5维的向量,有$$$2^5$$$种情况,要求所有向量在这种情况下的值,所以复杂度就是$$$O(2^5 \times n)$$$,推广到k维的情况下,复杂度就是$$$O(2^kn)$$$,而先前暴力的做法复杂度则为$$$O(\frac{kn(n-1)}{2})=O(kn^2)$$$。优化到这个程度,就能在限定时间求出答案了
说完了最大曼哈顿距离怎么计算,再来看hdu的这道题是不是就容易多了,还是同样的把绝对值打开来计算,只是在每种情况下,向量的大小不光是后面k个数计算的结果,还要再加上一个常数,其他的过程都是类似的。
#include<stdio.h>
typedef long long LL;
int first[][], second[][];//武器的副属性
int wf[],ws[];//武器的主属性
LL sf[],ss[];//求和需要用long long 保存 int n,m,k; int main(){
int kase;
LL mf,ms,ans;
int t;
for(scanf("%d",&kase);kase;kase--){
scanf("%d %d %d",&n,&m,&k);
for(int i=;i<n;++i)
for(scanf("%d",wf+i),t=;t<k;++t)
scanf("%d",first[i]+t);
for(int i=;i<m;++i)
for(scanf("%d",ws+i),t=;t<k;++t)
scanf("%d",second[i]+t);
ans=;
for(int i=(<<k)-;i>=;--i){//用二进制代表系数的序列,并遍历11111~00000
for(t=;t<n;++t)sf[t]=;
for(t=;t<m;++t)ss[t]=;
for(int w=,j=;j<k;j++,w<<=){//计算在当前序列下的值
for(t=;t<n;++t)
sf[t]=sf[t]+first[t][j]*(i&w?1.0:-1.0);
for(t=;t<m;++t)
ss[t]=ss[t]+second[t][j]*(i&w?1.0:-1.0);
}
//还要加上一个常数才是最终大小
mf=sf[]+wf[];
for(t=;t<n;++t)
if(mf<sf[t]+wf[t])mf=sf[t]+wf[t];
ms=ss[]-ws[];
for(t=;t<m;++t)
if(ms>ss[t]-ws[t])ms=ss[t]-ws[t];
if(ans<mf-ms)ans=mf-ms;
}
printf("%lld\n",ans ); }
}
hdu 6435 CSGO(最大曼哈顿距离)的更多相关文章
- HDU 4666 最远曼哈顿距离
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4666 关于最远曼哈顿距离的介绍: http://blog.csdn.net/taozifish/ar ...
- hdu 6435 CSGO
题意:现在有n个主武器, m个副武器, 你要选择1个主武器,1个副武器, 使得 题目给定的那个式子最大. 题解:这个题目困难的地方就在于有绝对值,| a - b | 我们将绝对值去掉之后 他的值就为 ...
- 2018 Multi-University Training Contest 10 CSGO(HDU - 6435)(最远曼哈顿距离)
有 n 种主武器,m 种副武器.每种武器有一个基础分数k种属性值 X[i] . 选出一种主武器 mw 和一种副武器 sw,使得两种武器的分数和 + 每个属性的差值尽量大.(参考下面的式子) 多维的最远 ...
- HDU - 6435 Problem J. CSGO (曼哈顿距离变换)
题目大意:有两类武器(主武器和副武器),每类有若干把,每把武器都有一个基础属性S,以及k个附加属性,让你选一把主武器M和一把副武器S,使得最大. 显然后面的和式是一个k维的曼哈顿距离,带绝对值符号不好 ...
- hdu 4666:Hyperspace(最远曼哈顿距离 + STL使用)
Hyperspace Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tota ...
- Hdu 4312-Meeting point-2 切比雪夫距离,曼哈顿距离,前缀和
题目: http://acm.hdu.edu.cn/showproblem.php?pid=4312 Meeting point-2 Time Limit: 2000/1000 MS (Java/Ot ...
- Hdu 4311-Meeting point-1 曼哈顿距离,前缀和
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4311 Meeting point-1 Time Limit: 2000/1000 MS (Java/Oth ...
- HDU 4312 Meeting point-2(切比雪夫距离转曼哈顿距离)
http://acm.hdu.edu.cn/showproblem.php?pid=4312 题意:在上一题的基础上,由四个方向改为了八个方向. 思路: 引用自http://blog.csdn.net ...
- HDU 4311 Meeting point-1(曼哈顿距离最小)
http://acm.hdu.edu.cn/showproblem.php?pid=4311 题意:在二维坐标中有n个点,现在要从这n个点中选出一个点,使得其他点到该点的曼哈顿距离总和最小. 思路: ...
随机推荐
- 20155334 2016-2017-2 《Java程序设计》第四周学习总结
20155334 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 第六章:继承与多态 继承:面对对象中,子类继承父类,避免重复的行为定义 extends表示会 ...
- vagrant boxes
vagrant box add hashicorp/precise64
- Using Xpath With Default XML Namespace in C#
If you have a XML file without any prefix in the namespace: <bookstore xmlns="http://www.con ...
- 北京Uber优步司机奖励政策(4月12日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- [css 揭秘] :CSS揭秘 技巧(五):条纹背景
条纹背景 https://github.com/FannieGirl/ifannie/问题:条纹背景 在设觉设计中无处不在,我们真的可以用css 创建图案吗? 这一章相对还是比较复杂的哦!一起get. ...
- exe4j 使用记录(二):jar打包exe
一.环境 exe4j: 6.0.2 jre(32位): 1.8 二.打包过程 1.新建一个文件夹testExe(我的目录位置:D:\testExe)用来存放所需要打成exe的jar包.jdk或者jre ...
- python yagmail第三方库发送邮件--更简洁
1.安装第三方库yagmail: pip install yagmail 2.上代码 import yagmail import os def send_email(): #链接邮箱服务器 serve ...
- TPO-17 C1 Find materials for an opera paper
TPO-17 C1 Find materials for an opera paper production n. 成果:产品:生产:作品 第 1 段 1.Listen to a conversati ...
- spark重点知识
1 scala 2 spark rdd 3 sprak core 4 性能优化 5 spark sql 6 spark streaming 掌握程度-精通的理解:
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...