首页 > 代码库 > POJ - 2926 Requirements

POJ - 2926 Requirements

题意:多维的曼哈顿最远距离

思路:做了1,2,3维的,其实就是枚举所有绝对值的可能的表达式

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 100005;
const int M = 5;
const double inf = 1e200;

struct node{
	double x[M];
}p[MAXN];
int n;
double minx[1<<M],maxx[1<<M];

double solve(){
	int i,j,k,t;
	int tmp = 1<<M;
	double s,ans = -inf;
	for (i = 0; i < tmp; i++){
		minx[i] = inf;
		maxx[i] = -inf;
	}
	for (i = 0; i < n; i++)
		for (j = 0; j < tmp; j++){
			t = j,s = 0;
			for (k = 0; k < M; k++){
				if (t & 1)
					s += p[i].x[k];
				else s -= p[i].x[k];
				t >>= 1;
			}
			if (maxx[j] < s)
				maxx[j] = s;
			if (minx[j] > s)
				minx[j] = s;
		}
	for (i = 0; i < tmp; i++)
		if (maxx[i]-minx[i] > ans)
			ans = maxx[i] - minx[i];
	return ans;
}

int main(){
	while (scanf("%d", &n) != EOF){
		for (int i = 0; i < n; i++)
			for (int j = 0; j < M; j++)
				scanf("%lf", &p[i].x[j]);
		printf("%.2f\n",solve());
	}
	return 0;
}