版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tianwei0822/article/details/81975789
题目:点击打开链接
题意:有n个主武器,m个副武器,(n < 1e5,m < 1e5)每个武器有个主属性S和一个 K(K < 5)个副属性,现在要求以下值最大
分析:对应两个属性值绝对值之差,|a-b|=a-b或者b-a,k小于等于5,所以两个武器的和的可能情况最多是2的k次方,32种情况。每一种状态,取n个主武器,属性和与s之和,中的最大值,与对应状态的m个副武器,属性和与s之和,中的最大值,两个最大值相加,在所有状态中取得得最大值,就是答案了。其实本质就是用一个数二进制表示一种状态,取反二进制表示减号,很巧妙,加深了对位运算的理解。
代码:
#pragma comment(linker, "/STACK:102400000,102400000")
#include<algorithm>
#include<iostream>
#include<fstream>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<iomanip>
#include<string>
#include<cstdio>
#include<bitset>
#include<vector>
#include<cctype>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<map>
using namespace std;
#define pt(a) cout<<a<<endl
#define debug test
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
typedef pair<int,int> PII;
const ll mod = 1e9+7;
const int N = 1e6+10;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qp(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
ll t,n,m,k,a[10],w[1<<7];
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>t;
while(t--) {
mst(w,0);///注意清空
cin>>n>>m>>k;
ll s,tp,ans=0;
rep(i,1,n) {
cin>>s;
rep(j,0,k-1) cin>>a[j];
rep(j,0,(1<<k)-1) {
tp=s;
rep(l,0,k-1) {
if(j&(1<<l)) tp+=a[l];
else tp-=a[l];
}
w[j]=max(w[j],tp);
}
}
rep(i,1,m) {
cin>>s;
rep(j,0,k-1) cin>>a[j];
rep(j,0,(1<<k)-1) {
tp=s;
rep(l,0,k-1) {
if(j&(1<<l)) tp-=a[l];///注意和上一次取反
else tp+=a[l];
}
ans=max(ans,tp+w[j]);
}
}
cout<<ans<<endl;
}
return 0;
}