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的个数


代码如下:

  1. //首先将前两列任意两项相加得到数组x,再将后两列任意两项相加取反得到数组y,
  2. //再将y排序,最后依次将x中的元素在y中进行
  3. //二分查找,看有多少个相等的数加起来即为最终的结果。
  4.  
  5. #include <iostream>
  6. #include <cstdio>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. int a[][];
  12. int x[];
  13. int y[];
  14. int ll, ans;
  15.  
  16. //search x[i] from array y[i]
  17. void bit_search(int t)
  18. {
  19. int mid,l = ,r = ll - ;
  20. while(l < r)
  21. {
  22. mid = (l+r) >> ;
  23. if(y[mid] < t)
  24. l = mid + ;
  25. else
  26. r = mid;
  27. }
  28. while(y[l] == t && l < ll) //可能有找到不止一个
  29. {
  30. ans++;
  31. l++;
  32. }
  33. }
  34.  
  35. int main()
  36. {
  37. int n,i,j;
  38.  
  39. while(cin >> n)
  40. {
  41. ans = ,ll = ;
  42.  
  43. for(i = ; i < n; i++) //record the data
  44. cin >> a[i][] >> a[i][]
  45. >> a[i][] >> a[i][];
  46.  
  47. for(i = ; i < n; i++) //枚举左侧
  48. {
  49. for(j = ; j < n ; j++)
  50. {
  51. x[ll++] = a[i][] + a[j][];
  52. }
  53. }
  54.  
  55. ll = ;
  56.  
  57. for(i = ; i < n ; i++) //枚举右侧
  58. {
  59. for(j = ; j < n ; j++)
  60. {
  61. y[ll++] = -(a[i][] + a[j][]); //这里取反 a + b + c + d = 0等价于a + b = -(c + d);
  62. }
  63. }
  64. sort(y,y+ll); //先排序
  65.  
  66. for(i = ; i < ll ; i++) //再进行二分查找,如果找到那么ans++
  67. bit_search(x[i]);
  68.  
  69. cout << ans << endl;
  70. }
  71. return ;
  72. }

POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找的更多相关文章

  1. POJ - 2785 4 Values whose Sum is 0 二分

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25615   Accep ...

  2. POJ 2785 4 Values whose Sum is 0(折半枚举+二分)

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25675   Accep ...

  3. 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 ...

  4. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  5. POJ 2785 4 Values whose Sum is 0

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 13069   Accep ...

  6. 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 ...

  7. POJ 2785 4 Values whose Sum is 0(哈希表)

    [题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...

  8. 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 ...

  9. [POJ] 2785 4 Values whose Sum is 0(双向搜索)

    题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...

随机推荐

  1. bzoj 1415(概率dp和bfs预处理)

    感觉挺经典的一道题目. 先用 bfs 预处理下一步走到的位置.因为每一步走法都是固定的,所以可以用dp的方法来做. 1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec  M ...

  2. [Go语言]从Docker源码学习Go——指针和Structs

    这两天在看reflect这个包在Docker中的使用时,遇到了各种问题,最后虽然知道怎么用了. 但是对于这块的原理还不是太懂,于是把"THE WAY TO GO"中关键的几章看了下 ...

  3. angular-ui-bootstrap 日历控件国际化

    angularjs-angular-ui-bootstrap-changing-language http://stackoverflow.com/questions/19671887/angular ...

  4. linux时间格式化

    echo `date +'[%Y-%m-%d %H:%M:%S]'`

  5. wordpress添加关键字

    wordpress自动添加标签为关键字: <?php //判断是否为首页 if ( is_home ()) { $description = "jcomey一个文艺青年的个人博客&qu ...

  6. 系统根据用户cookies,为用户打上各种标签

    DSP营销学院_品友学院 | 品友推广官网 http://e.ipinyou.com/school_article40.html 智能算法+动态出价=最大发挥推广费用的价值 针对每一个曝光进行甄别和竞 ...

  7. MySQL小记

    一.MyISAM和InnoDB MyISAM引擎是不支持事务的,所以一般开发Mysql的引擎使用InnoDB. 事务处理上方面: MyISAM类型的表强调的是性能,其执行速度比InnoDB类型更快,但 ...

  8. 【sed / awk脚本编写】

    awk awk分为BEGIN部分,正则匹配部分,END部分三部分. 我一般在BEGIN部分定义一些变量,正则部分用于匹配和执行一些解析和统计,END部分用于输出结果. 总体结构: awk 'BEGIN ...

  9. shell脚本杂

    1.sh -x 跟踪shell脚本中的每个命令 [root@master shellexer]# cat bash.sh #!/bin/bash var=$ echo $var [root@maste ...

  10. 我的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 ...