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个点中选出一个点,使得其他点到该点的曼哈顿距离总和最小. 思路: ...
随机推荐
- 考研编程练习----Prim算法的c语言实现
本文引用自泽爷工作室http://www.zeyes.org/study/clang/189.html 算法思想: 1.在把生成树看成一个集合(开始集合为空,到各个结点的距离当然未知) 2.结点与集合 ...
- SublimeText 改变 tab的距离
view -> Indentation -> Tab width ……
- java开发工具使用
一.MyEclipse软件的使用 1)ctrl+n新建文件 2)ctrl+d删除一行 3)alt+/提示补齐 (main/syso/syse/for遍历最近的数组) 4)ctrl+shift+f格式化 ...
- day 2 函数的嵌套
1.函数的嵌套 百度百科 2.产品如何运作 3.应用 1)版本1:打印1条线 #1.定义函数 def print_line(): print("-"*50) #2.调用函数 ...
- CF 1025 D. Recovering BST
D. Recovering BST http://codeforces.com/contest/1025/problem/D 题意: 给出一个连续上升的序列a,两个点之间有边满足gcd(ai ,aj) ...
- STM8在IAR中Printf的整形长度问题
//ld是32位的 printf("up_intval:%ld\r\n",device_set.upload_tem); //d是16位的 printf("up_intv ...
- 【JUC源码解析】ConcurrentHashMap
简介 支持并发的哈希表.其中包括红黑树,扩容,分槽计数等知识点. 源码分析 常量 private static final int MAXIMUM_CAPACITY = 1 << 30; ...
- 探究linux设备驱动模型之——platform虚拟总线(一)
说在前面的话 : 设备驱动模型系列的文章主要依据的内核版本是2.6.32的,因为我装的Linux系统差不多就是这个版本的(实际上我用的fedora 14的内核版本是2.6.35.13的.) ...
- Java构造方法与析构方法实例剖析
Java构造方法 类有一个特殊的成员方法叫作构造方法,它的作用是创建对象并初始化成员变量.在创建对象时,会自动调用类的构造方法. 构造方法定义规则:Java 中的构造方法必须与该类具有相同的名字,并且 ...
- 【转】自动化测试 - Appium + Python史上最全最简环境搭建步骤
一,为什么是Appium借一张图: 1.1 Appium优点 l 开源 l 跨架构:NativeApp.Hybird App.Web App l 跨设备:Android.iOS.Firefox ...