北京邀请赛 H. Happy Reversal

题意:给你一些二进制的数,然后你可以选择按位取反,也可以不变,你只能选择一种,然后让你找出最大和最小,求最大的差值

思路:将取反与不取反都算出来,然后大的放一边,小的放一边,排序后判断

#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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值