POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找
2017-08-01 21:29:14
writer:pprp
参考:http://blog.csdn.net/piaocoder/article/details/45584763
算法分析:直接暴力复杂度过高,所以要用二分的方法,分成两半复杂度就会大大降低;
题目意思:给定4个n(1<=n<=4000)元素的集合 A、B、C、D ,从4个集合中分别选取一个元素a, b,c,d。求满足 a+b+c+d=0的个数
代码如下:
- //首先将前两列任意两项相加得到数组x,再将后两列任意两项相加取反得到数组y,
- //再将y排序,最后依次将x中的元素在y中进行
- //二分查找,看有多少个相等的数加起来即为最终的结果。
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- int a[][];
- int x[];
- int y[];
- int ll, ans;
- //search x[i] from array y[i]
- void bit_search(int t)
- {
- int mid,l = ,r = ll - ;
- while(l < r)
- {
- mid = (l+r) >> ;
- if(y[mid] < t)
- l = mid + ;
- else
- r = mid;
- }
- while(y[l] == t && l < ll) //可能有找到不止一个
- {
- ans++;
- l++;
- }
- }
- int main()
- {
- int n,i,j;
- while(cin >> n)
- {
- ans = ,ll = ;
- for(i = ; i < n; i++) //record the data
- cin >> a[i][] >> a[i][]
- >> a[i][] >> a[i][];
- for(i = ; i < n; i++) //枚举左侧
- {
- for(j = ; j < n ; j++)
- {
- x[ll++] = a[i][] + a[j][];
- }
- }
- ll = ;
- for(i = ; i < n ; i++) //枚举右侧
- {
- for(j = ; j < n ; j++)
- {
- y[ll++] = -(a[i][] + a[j][]); //这里取反 a + b + c + d = 0等价于a + b = -(c + d);
- }
- }
- sort(y,y+ll); //先排序
- for(i = ; i < ll ; i++) //再进行二分查找,如果找到那么ans++
- bit_search(x[i]);
- cout << ans << endl;
- }
- return ;
- }
POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找的更多相关文章
- POJ - 2785 4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25615 Accep ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
- poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))
Description The SUM problem can be formulated . In the following, we assume that all lists have the ...
- POJ 2785 4 Values whose Sum is 0(想法题)
传送门 4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 20334 A ...
- POJ 2785 4 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 13069 Accep ...
- POJ 2785 4 Values whose Sum is 0(暴力枚举的优化策略)
题目链接: https://cn.vjudge.net/problem/POJ-2785 The SUM problem can be formulated as follows: given fou ...
- POJ 2785 4 Values whose Sum is 0(哈希表)
[题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...
- POJ 2785 4 Values whose Sum is 0 Hash!
http://poj.org/problem?id=2785 题目大意: 给你四个数组a,b,c,d求满足a+b+c+d=0的个数 其中a,b,c,d可能高达2^28 思路: 嗯,没错,和上次的 HD ...
- [POJ] 2785 4 Values whose Sum is 0(双向搜索)
题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...
随机推荐
- bzoj 1415(概率dp和bfs预处理)
感觉挺经典的一道题目. 先用 bfs 预处理下一步走到的位置.因为每一步走法都是固定的,所以可以用dp的方法来做. 1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec M ...
- [Go语言]从Docker源码学习Go——指针和Structs
这两天在看reflect这个包在Docker中的使用时,遇到了各种问题,最后虽然知道怎么用了. 但是对于这块的原理还不是太懂,于是把"THE WAY TO GO"中关键的几章看了下 ...
- angular-ui-bootstrap 日历控件国际化
angularjs-angular-ui-bootstrap-changing-language http://stackoverflow.com/questions/19671887/angular ...
- linux时间格式化
echo `date +'[%Y-%m-%d %H:%M:%S]'`
- wordpress添加关键字
wordpress自动添加标签为关键字: <?php //判断是否为首页 if ( is_home ()) { $description = "jcomey一个文艺青年的个人博客&qu ...
- 系统根据用户cookies,为用户打上各种标签
DSP营销学院_品友学院 | 品友推广官网 http://e.ipinyou.com/school_article40.html 智能算法+动态出价=最大发挥推广费用的价值 针对每一个曝光进行甄别和竞 ...
- MySQL小记
一.MyISAM和InnoDB MyISAM引擎是不支持事务的,所以一般开发Mysql的引擎使用InnoDB. 事务处理上方面: MyISAM类型的表强调的是性能,其执行速度比InnoDB类型更快,但 ...
- 【sed / awk脚本编写】
awk awk分为BEGIN部分,正则匹配部分,END部分三部分. 我一般在BEGIN部分定义一些变量,正则部分用于匹配和执行一些解析和统计,END部分用于输出结果. 总体结构: awk 'BEGIN ...
- shell脚本杂
1.sh -x 跟踪shell脚本中的每个命令 [root@master shellexer]# cat bash.sh #!/bin/bash var=$ echo $var [root@maste ...
- 我的Android进阶之旅------>解决Android Studio编译后安装apk报错:The APK file does not exist on disk
1.错误描述 今天用Android Studio编译应用后安装APK的时候,报错了,错误如下所示: The APK file build\outputs\apk\OYP_2.3.4_I2Base_64 ...