您的位置:首页 > 其它

给40亿个不重复的unsigned int的数,没排序,然后再给一个数,如何快速间断这个数是否在那40亿个数中

2015-09-08 20:43 501 查看
40亿个数,如果用无符号的long long数组来存,那么使用数组里的每一个元素的每一位代表一个数,具体为:

a[0] ---- 0~63

a[1] ---- 64~127

a[2] ---- 128~190

...

那么,40亿 bit/64 = 6.25*107 *8 byte = 500MB , 内存就满足了。

#include <iostream>
using namespace std;

#define maxn 62500000
unsigned long long a[maxn];

int main() {
unsigned n, m, x;
while (cin >> n >> m) {
memset(a, 0, sizeof(a));
for (int i=0; i<n; i++) {
cin >> x;
a[ x>>5 ] |= 1 << (x & 0x3F);
}
for (int i=0; i<m; i++) {
cin >> x;
if ( a[ x>>5 ] & ( 1 << (x & 0x3F) ) ) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
}
}


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