2800 BitSet3(eden)
来源:互联网 发布:淘宝助理5.8.5.0下载 编辑:程序博客网 时间:2024/06/07 17:51
Description
Give you N numbers a[1]…a[n]
and M numbers b[1]…b[m]
For each b[k], if we can find i,j a[i] + a[j] = b[k] or a[i] = b[k] , we say k is a good number.
And you should only output the number of good numbers.
0 < n, m, a[i], b[j] <= 200000
Sample Input
3 6135245789
Sample Output
4
b[1]…b[m] 2,4,5,7,8,9
2 = 1+1
4 = 1+3
5 = 5
8 = 3+5
Hint
虽然数据范围为200000,但因为eden的特殊原因,实际数据并没有这么大
详细分析,首先如果b[k]在a数组中出现,那么这个k是符合条件的
对于每个b[k]枚举i,j看能否满足a[i]+a[j]=a[k] 的做法应该会超时。但是O(n*m)的做法可能可以过。
有一种利用bitset的非标准做法。利用bitset记录哪些数在a中出现过了。那么把这个bitset左移1位,我们就可以得到有哪些x(x=a[i]+1)出现过了。左移两位,就知道哪些x(x = a[i]+2)出现过。如果此处左移a[1]那么,得到的就是a[1]+a[1] ,… ,a[1]+a[n]这些数字的bitset。枚举a[1]到a[n]来左移,把结果取或,就能得到a[i]+a[j]的集合,再或上a[1]..a[n]的bitset,把这个结果和b[1]..b[m]的bitset取&, 剩下的这个bitset有多少个1,答案就是几。
另外有一个技巧,我们先把a数组排序(题目并没有保证a数组有序)对于一个集合,先左移a[1]次,之后都把之前的答案左移a[i]-a[i-1]次。
Author: 王毅峰
Submission
main.cpp
#include <iostream>#include <bitset>using namespace std;int main(){ bitset<200000> a,b,tem; int m,n; cin>>m>>n; int save[m]; for (int i = 0; i < m; ++i){ int num; cin>>num; a.set(num); save[i]=num; } for (int i = 0; i < n; ++i){ int num; cin>>num; b.set(num); } tem=a; for (int i = 0; i < m; ++i) a=a|(tem<<save[i]); b=b&a; cout<<b.count()<<endl; return 0;}
- 2800 BitSet3(eden)
- Eden&Survivor
- Uva10001 Garden of Eden
- 10001 - Garden of Eden
- uva10001 - Garden of Eden
- jvm crash eden 100%
- UVA10001Garden of Eden
- uva10001 Garden of Eden
- hdu5977 Garden of Eden
- 1802 Set Operations (eden)
- 2736 FunctionTemplate(eden)
- 2740 Template Compare (eden)
- 1810 Bitset (eden)
- 1811 Matrix Calculation (eden)
- 2736 FunctionTemplate(eden)
- hdu5977 Garden of Eden
- HDU5977-Garden of Eden
- UVa 10001 - Garden of Eden
- IMWeb提升营Day5 | 训练题25:复杂链表的复制
- [bzoj 3270] 博物馆:高斯消元+期望转移
- 1289 大鱼吃小鱼
- 移动端产品盈利/推广
- 对于面对对象和prototype的理解
- 2800 BitSet3(eden)
- java JNDI
- node学习篇——mongodb个人理解
- Android基本控件的使用
- Java 8 的 Nashorn 脚本引擎教程
- 通过修改host解决Google打不开
- 树->创建并遍历二叉树
- JAVA简单快速排序讲解
- matlab批量转化img到gray