二进制状态压缩
abs(a - b) = max(a - b, b - a)
通过上式我们可以发现,对于选中的武器的每个属性,主武器如果是+,那么服务器必定是-,反之也是一样。
我们对所有绝对值求和其实就是把每个数前面带上符号相加。比如abs(a-b)+abs(c-d),(a-b>b-a, d-c>c-d) = a-b+d-c = a-c-b+d
所以我们可以枚举所有武器的每种属性的符号,求出他们和的最大值,最后在把副武器状态取反相加就可以了。
#include#define INF 0x3f3f3f3f#define full(a, b) memset(a, b, sizeof a)#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)using namespace std;typedef long long ll;inline int lowbit(int x){ return x & (-x); }inline int read(){ int ret = 0, w = 0; char ch = 0; while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = getchar(); return w ? -ret : ret;}inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }inline int lcm(int a, int b){ return a / gcd(a, b) * b; }template inline T max(T x, T y, T z){ return max(max(x, y), z); }template inline T min(T x, T y, T z){ return min(min(x, y), z); }template inline A fpow(A x, B p, C lyd){ A ans = 1; for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd; return ans;}const int N = 100005;int _, n, m, k, sc[N];ll mw[40], sw[40];int main(){ for(_ = read(); _; _ --){ for(int i = 0; i < 40; i ++) mw[i] = sw[i] = -INF; n = read(), m = read(), k = read(); for(int i = 1; i <= n; i ++){ int basic = read(); for(int j = 1; j <= k; j ++) sc[j] = read(); for(int j = 0; j < (1 << k); j ++){ ll val = basic; for(int t = 0; t < k; t ++){ if((j >> t) & 1) val += sc[t + 1]; else val -= sc[t + 1]; } mw[j] = max(mw[j], val); } } for(int i = 1; i <= m; i ++){ int basic = read(); for(int j = 1; j <= k; j ++) sc[j] = read(); for(int j = 0; j < (1 << k); j ++){ ll val = basic; for(int t = 0; t < k; t ++){ if((j >> t) & 1) val += sc[t + 1]; else val -= sc[t + 1]; } sw[j] = max(sw[j], val); } } ll ans = -INF; for(int i = 0; i < (1 << k); i ++){ ans = max(ans, mw[i] + sw[(1 << k) - 1 - i]); } printf("%lld\n", ans); } return 0;}