zoukankan      html  css  js  c++  java
  • *[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;
    }
    

      

  • 相关阅读:
    计算机网络基础 汇总
    指针与数组
    卡特兰数
    Leetcode Sort Colors
    Leetcode Group Shifted Strings
    Leetcode Summary Ranges
    Leetcode Count Primes
    Leetcode Reverse Words in a String II
    Leetcode Reverse Words in a String
    Leetcode Rotate Array
  • 原文地址:https://www.cnblogs.com/lautsie/p/3909001.html
Copyright © 2011-2022 走看看