hdu 6435 Problem J. CSGO(多维最远曼哈顿距离)

题目:在两个点集内各选一点求最远曼哈顿距离。

思路:把绝对值去掉后有2^k次方种可能的情况,都枚举一遍。

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
const ll inf = 1e12;
struct point{
    ll x[8];
}p1[maxn],p2[maxn];
int t,n,m,dem;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&dem);
        dem++;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<dem;j++)
                scanf("%lld",&p1[i].x[j]);
        }

        for(int i=0;i<m;i++)
        {
            for(int j=0;j<dem;j++)
                scanf("%lld",&p2[i].x[j]);
            p2[i].x[0]=-p2[i].x[0];
        }

        int s = 1<<dem;
        ll ans = -inf,tmp;
        for(int i=0;i<s;i++)
        {
            ll max1 = -inf;
            ll max2 = -inf;
            for(int j=0;j<n;j++)
            {
               tmp = 0;
               for(int k=0;k<dem;k++)
               {
                   if((1<<k)&i)
                        tmp+=p1[j].x[k];
                   else
                        tmp-=p1[j].x[k];
               }
               max1 = max(max1,tmp);
            }
            for(int j=0;j<m;j++)
            {
                tmp = 0;
                for(int k=0;k<dem;k++)
                {
                    if((1<<k)&i)
                        tmp-=p2[j].x[k];
                    else
                        tmp+=p2[j].x[k];
                }
                max2 = max(max2,tmp);
            }
            ans = max(ans,max1+max2);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/dllpXFire/article/details/82024734