转自:https://blog.csdn.net/qq_40774175/article/details/81950796
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6435
思路:
因为需要从主武器和副武器里挑出一个。对于属性的差值,最好的方法肯定是一个最大的减去最小的。
所以可以这样考虑,对于每种属性,它对答案的贡献,要么是加上它,要么是减去它,所以只要枚举每种属性的加减状态就行。
对于主武器的s,和副武器的s,也可以将他们考虑进来,只要将他们的位置错开(这样就可以保证他们不会相减了),将主s设在0位置,副武器s设在1位置即可,最后就是2^(k+2)的枚举。
ac代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+9;
int n,m,k;
ll a[maxn][8],b[maxn][8];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T;
cin>>T;
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<n;i++)
{
scanf("%lld",&a[i][0]);
for(int j=0;j<k;j++)
{
scanf("%lld",&a[i][j+2]);
}
}
for(int i=0;i<m;i++)
{
scanf("%lld",&b[i][1]);
for(int j=0;j<k;j++)
{
scanf("%lld",&b[i][j+2]);
}
}
int up=1<<(k+2);
ll ans=0;
for(int s=0;s<up;s++)
{
ll maxv1=-1e18,minv1=1e18;
ll maxv2=-1e18,minv2=1e18;
for(int i=0;i<n;i++)
{
ll tmp=0;
for(int t=0;t<k+2;t++)
{
if(s&(1<<t))
{
tmp+=a[i][t];
}
else
{
tmp-=a[i][t];
}
}
maxv1=max(maxv1,tmp);
minv1=min(minv1,tmp);
}
for(int i=0;i<m;i++)
{
ll tmp=0;
for(int t=0;t<(k+2);t++)
{
if(s&(1<<t))
{
tmp+=b[i][t];
}
else
{
tmp-=b[i][t];
}
}
maxv2=max(maxv2,tmp);
minv2=min(minv2,tmp);
}
ans=max(ans,max(maxv1-minv2,maxv2-minv1));
}
printf("%lld\n",ans);
}
}