HDU 6435 CSGO 求多维曼哈顿最远距离

题意:武器有一个能力值(S_i)和K个属性值(X_i),要求从主武器,副武器两个集合中各挑选一把使得评分最高。评分的指二者的S值之和,及二者各项属性值的差的绝对值的和

分析:在做这道题前,可以先考虑POJ 2926
求的是5维的n个点最远点对的距离

我们先从只有一维的情况考虑起。只需要获得该维的最大值,最小值,做差即可。
再考虑二维的情况,我们是否能够化为一个形式,使得无需考虑绝对值,直接用最大最小值做差呢
|x1-x2|+|y1-y2|, 必然为以下四者中的一种
(x1-y1)-(x2-y2) , (x1+y1)-(x2+y2), (-x1-y1)-(-x2-y2), (-x1+y1)-(-x2+y2)
注意到括号内形式相同,也就是说,对于二维的情况,我们分四种情况讨论,分别求出最值,最后做差即可。
同理推广到五维
在写法上可利用数位表示状态

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;

const int maxn = 1e5 + 10;
const double INF = 1e12;

int n;
double A[maxn][5];
double ans, _min, _max;

int main() {
	while (~scanf("%d", &n)) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < 5; j++) {
				scanf("%lf", &A[i][j]);
			}
		}
		ans = 0;
		for (int s = 0; s < (1 << 5); s++) {
			_min = INF; _max = -INF;
			for (int i = 0; i < n; i++) {
				double t = 0;
				for (int j = 0; j < 5; j++) {
					if ((s >> j) & 1) t += A[i][j];
					else t -= A[i][j];
				}
				_min = min(_min, t);
				_max = max(_max, t);
			}
			ans = max(ans, _max - _min);
		}
		printf("%.2lf
", ans);
	}

	return 0;
}

在考虑原题,考虑S应为相加的条件,我们如何利用已有结论呢?
显然将主武器的S取为负数即可, 那么问题则变为K+1维。
同时为了保证求得的最远点对分别属于两个集合,主武器的S再减去一个足够大的数即可

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;

int N, M, K;

const LL dep = 1e12;
const LL INF = 1e14;
const int maxn = 2e5 + 10;
LL wp[maxn][6];
LL ans, _min, _max;

int main() {
	int T; scanf("%d", &T);
	while (T--) {
		scanf("%d%d%d", &N, &M, &K);
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < K + 1; j++) {
				scanf("%lld", &wp[i][j]);
			}
			wp[i][0] = -dep - wp[i][0];
		}
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < K + 1; j++) {
				scanf("%lld", &wp[i + N][j]);
			}
		}
		ans = 0;
		for (int s = 0; s < (1 << (K + 1)); s++) {
			_min = INF; _max = -INF;
			for (int i = 0; i < N + M; i++) {
				LL t = 0;
				for (int j = 0; j < (K + 1); j++) {
					if ((s >> j) & 1) t += wp[i][j];
					else 			  t -= wp[i][j];
				}
				_min = min(_min, t);
				_max = max(_max, t);
			}
			ans = max(ans, _max - _min);
		}
		printf("%lld
", ans - dep);
	}

	return 0;
}
原文地址:https://www.cnblogs.com/xFANx/p/9521996.html