题意:给你一些二进制的数,然后你可以选择按位取反,也可以不变,你只能选择一种,然后让你找出最大和最小,求最大的差值
思路:将取反与不取反都算出来,然后大的放一边,小的放一边,排序后判断
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int MAXN = 100010;
struct node {
ll x;
int sign;
}arr[MAXN],brr[MAXN];
int t,n,k;
ll Max,Min;
char str[70];
int cmp(node a, node b) {
return a.x < b.x;
}
int main() {
int cas = 1;
scanf("%d", &t);
while (t--) {
scanf("%d%d%*c", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%s", str);
ll cnt1 = 0,cnt2 = 0;
for (int j = 0; j < k; j++) {
if (str[j] == '0')
cnt1 += (ll)1<<(k-j-1);
if (str[j] == '1')
cnt2 += (ll)1<<(k-j-1);
}
if (cnt1 < cnt2)
swap(cnt1, cnt2);
arr[i].x = cnt1;
brr[i].x = cnt2;
brr[i].sign = arr[i].sign = i;
}
printf("Case #%d: ", cas++);
if (n == 1) {
printf("0\n");
continue;
}
sort(arr, arr+n, cmp);
sort(brr, brr+n, cmp);
if (arr[n-1].sign != brr[0].sign) {
Max = arr[n-1].x;
Min = brr[0].x;
}
else if (arr[n-1].x-brr[1].x > arr[n-2].x-brr[0].x) {
Max = arr[n-1].x;
Min = brr[1].x;
}
else {
Max = arr[n-2].x;
Min = brr[0].x;
}
printf("%lld\n", Max-Min);
}
return 0;
}