您的位置:首页 > 其它

*[hackerrank]ACM ICPC Team

2014-08-12 23:58 337 查看
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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: