*[hackerrank]ACM ICPC Team

https://www.hackerrank.com/contests/w6/challenges/acm-icpc-team

这道题在contest的时候数据量改小过,原来的数据量需要进行优化才能过。参考了:http://chasethered.com/2014/07/hackerrank-weekly-challenge-6-problem-1/

首先是转化成int32的数组,然后N^N的复杂度两两比较求bit数。求bit中1的个数有几种做法:

- x & (x - 1)
- Hamming weight的经典求法,基于树状累加:http://en.wikipedia.org/wiki/Hamming_weight
- 内存足够大可以查表得;

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int bitCount(unsigned int u) {
   unsigned int uCount;
 
   uCount = u - ((u >> 1) & 033333333333) - ((u >> 2) & 011111111111);
   return ((uCount + (uCount >> 3)) & 030707070707) % 63;
}

int main() {
	int N;
	int M;
	cin >> N >> M;
	vector<vector<int>> bm(N);
	int len = (M - 1) % 32 + 1;
	for (int i = 0; i < N; i++) {
		bm[i].resize(len);
	}
	for (int i = 0; i < N; i++) {
		string s;
		cin >> s;
		int k = -1;
		for (int j = 0; j < M; j++) {
			if (j % 32 == 0)
				k++;
			bm[i][k] *= 2;
			bm[i][k] += (int) (s[j] - '0');
		}
	}
	int topicCount = 0;
	int teamCount = 0;
	for (int i = 0; i < N; i++) {
		for (int j = i + 1; j < N; j++) {
			int local = 0;
			for (int k = 0; k < len; k++) {
				unsigned int tmp = bm[i][k] | bm[j][k];
				int count = bitCount(tmp);
				local += count;
			}
			if (local > topicCount) {
				topicCount = local;
				teamCount = 1;
			} else if (local == topicCount) {
				teamCount++;
			}
		}
	}
	cout << topicCount << endl;
	cout << teamCount << endl;
	return 0;
}

  

转载于:https://www.cnblogs.com/lautsie/p/3909001.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值