The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .

Input

The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2 28 ) that belong respectively to A, B, C and D .

Output

For each input file, your program has to write the number quadruplets whose sum is zero.

Sample Input

  1. 6
  2. -45 22 42 -16
  3. -41 -27 56 30
  4. -36 53 -37 77
  5. -36 30 -75 -46
  6. 26 -38 -10 62
  7. -32 -54 -6 45

Sample Output

  1. 5

Hint

Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).

思路:和之前更新的校赛的题目差不多,都是枚举+二分查找

代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<iostream>
  5. using namespace std;
  6. #define maxn 4040
  7. int a[maxn],b[maxn],c[maxn],d[maxn],sum[maxn*maxn];
  8. int main()
  9. {
  10. int n,i,j;
  11. while(scanf("%d",&n)!=EOF)
  12. {
  13. for(i=0;i<n;++i)
  14. scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
  15. for(i=0;i<n;++i)
  16. {
  17. for(j=0;j<n;++j)
  18. sum[n*i+j]=c[i]+d[j];
  19. }
  20. sort(sum,sum+n*n);
  21. int ans=0;
  22. for(i=0;i<n;++i)
  23. {
  24. for(j=0;j<n;++j)
  25. {
  26. int k=-(a[i]+b[j]);
  27. ans+=upper_bound(sum,sum+n*n,k)-lower_bound(sum,sum+n*n,k);
  28. }
  29. }
  30. printf("%d\n",ans);
  31. }
  32. return 0;
  33. }

4 Values whose Sum is 0(枚举+二分)的更多相关文章

  1. UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)

    4 Values whose Sum is 0 题目链接:https://cn.vjudge.net/problem/UVA-1152 ——每天在线,欢迎留言谈论. 题目大意: 给定4个n(1< ...

  2. UVA-1152 4 Values whose Sum is 0 (二分)

    题目大意:在4个都有n个元素的集合中,每个集合选出一个元素,使得4个数和为0.问有几种方案. 题目分析:二分.任选两组求和,剩下两组求和,枚举第一组中每一个和sum,在第二组和中查找-sum的个数,累 ...

  3. 4 Values whose Sum is 0 (二分+排序)

    题目: The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, com ...

  4. POJ 2785 4 Values whose Sum is 0 (二分)题解

    思路: 如果用朴素的方法算O(n^4)超时,这里用折半二分.把数组分成两块,分别计算前后两个的和,然后枚举第一个再二分查找第二个中是否有满足和为0的数. 注意和有重复 #include<iost ...

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

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

  6. [poj2785]4 Values whose Sum is 0(hash或二分)

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

  7. 4 Values whose Sum is 0(二分)

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

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

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

  9. 折半枚举(双向搜索)poj27854 Values whose Sum is 0

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

  10. POJ 2785:4 Values whose Sum is 0 二分

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

随机推荐

  1. hibernate学习笔记(4)表单操作

    User.hbm.xml的表单配置: ①主键 <id name="id" type="java.lang.Integer"> <column ...

  2. SQLAchemy ORM框架

    SQLAchemy SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行S ...

  3. sqlserver 查询int类型 in (字符串) 报转换int类型出错的问题

    , , '') ) AS c_departNames FROM t_user AS A LEFT JOIN t_role AS B ON A.c_roleId=B.c_roleId 用 CHARIND ...

  4. [poj3348]Cows

    题目大意:求凸包面积. 解题关键:模板题,叉积求面积. 这里的cmp函数需要调试一下,虽然也对,与普通的思考方式不同. #include<cstdio> #include<cstri ...

  5. matlab基础功能实践

    一.matlab在高等数学中的应用(<数学建模算法与应用>P453) 1.求极限 syms x b=limit((sqrt(1+x^2)-1)/(1-cos(x))) syms x a b ...

  6. ubuntu下使用PIL中的show函数,无法显示图片的问题

    问题描述:ubuntu14.04系统,python2.7(version),正在学习python中, from PIL import Image im = Image.open('1.jpg') im ...

  7. aspose ppt转图片

    如果直接转图片,会很模糊采用先将ppt转pdf,在通过pdf转图片,这样出来的结果就非常清晰 var pptFileName = "公司网络及计算机使用与要求.pptx"; Pre ...

  8. 3D模型浏览器的实现思路

    前段时间正好浏览了数据结构中关于图的部分,突然就意识到一个问题,3D模型就是用无向图来存储的.仔细想一想是不是这样呢? 一个3D模型去掉材质之后剩下的部分就是点以及点和点之间的连线了,点我们用三维坐标 ...

  9. setex()

    设置值和有效期 $redis->setex($key, $expire, $value);//$expire,有效期,单位秒 相当于 SET key value//设置键值 EXPIRE key ...

  10. 3.4PCL中异常处理机制

    1.开发者如何增加一个新的异常类 2.如何使用自定义的异常 3.异常的处理