4 Values whose Sum is 0
Time Limit: 15000MS   Memory Limit: 228000K
Total Submissions: 25615   Accepted: 7697
Case Time Limit: 5000MS

Description

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 228 ) 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).

Source


题目大意:给4列数,每列有n个数字,从每列中取一个数,求四个数相加为零的情况有多少种。
思路:用两个数组,分别记录左边两列和右边两列的数相加的和,再将其中一个数组从小到大排列,将另一数组遍历一边,用二分的方法判断个数。
  1. #include<stdio.h>
  2. #include<algorithm>
  3. using namespace std;
  4. int a[4005][4], sum1[16000001], sum2[16000001];
  5. int main()
  6. {
  7. int n, mid;
  8.  
  9. while (~scanf("%d", &n))
  10. {
  11. int k = 0;
  12. for (int i = 0; i < n; i++)
  13. {
  14. scanf("%d %d %d %d", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);
  15. }
  16. for (int i = 0; i < n; i++)
  17. for (int j = 0; j < n; j++)
  18. {
  19. sum1[k] = a[i][0] + a[j][1];
  20. sum2[k++] = a[i][2] + a[j][3];
  21. }
  22. sort(sum1, sum1 + k);
  23.  
  24. int num = 0;
  25. for (int i = 0; i < k; i++)
  26. {
  27. int left = 0, right = k - 1;
  28. while (left <= right)
  29. {
  30. mid = (left + right) / 2;
  31. if (sum2[i] + sum1[mid] == 0)
  32. {
  33. num++;
  34. for (int j = mid + 1; j < k; j++)
  35. {
  36. if (sum2[i] + sum1[j] == 0)
  37. num++;
  38. else
  39. break;
  40. }
  41. for (int j = mid - 1; j >= 0; j--)
  42. {
  43. if (sum2[i] + sum1[j] == 0)
  44. num++;
  45. else
  46. break;
  47. }
  48. break;
  49. }
  50. if (sum2[i] + sum1[mid] > 0)
  51. right = mid - 1;
  52. else
  53. left = mid + 1;
  54. }
  55. }
  56. printf("%d\n", num);
  57. }
  58. return 0;
  59. }



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

  1. POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找

    2017-08-01 21:29:14 writer:pprp 参考:http://blog.csdn.net/piaocoder/article/details/45584763 算法分析:直接暴力 ...

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

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

  3. POJ 2785 4 Values whose Sum is 0

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Java入门系列(一)基础概览

    序言 Java语言的特点不使用指针而使用引用.  

  2. java与Excel (.xls文件) ---使用JXL创建,增添表格文件

    由于一些原因要搞一下excel文件,个人感觉poi太难,所以用了JXL(感觉比较简单). 1.添加外部归档 jxl.jar 2. /** 生成的xls文件第一次需要手动选择EXCEL打开* * */ ...

  3. 创建分区swap分区

    1.将文件系统卸载 #umount /sdc5 2.创建swap分区 #mkswap /dev/sdc5 3.激活swap分区 #swapon -a /dev/sdc5 4.查看swap分区情况 #s ...

  4. java map遍历并删除特定值

    删除map中包含password和username的键值对 若是在map中直接删除,会指针错误 Iterator<Map.Entry<String,Object>> it = ...

  5. aarch64_g1

    GAPDoc-1.5.1-12.fc26.noarch.rpm 2017-02-14 07:37 1.0M fedora Mirroring Project GAPDoc-latex-1.5.1-12 ...

  6. 企业日志大数据分析系统ELK+KAFKA实现【转】

    背景: 最近线上上了ELK,但是只用了一台Redis在中间作为消息队列,以减轻前端es集群的压力,Redis的集群解决方案暂时没有接触过,并且Redis作为消息队列并不是它的强项:所以最近将Redis ...

  7. MySQL异步复制、半同步复制详解

    MySQL数据复制的原理图大致如下: 从上图我们可以看出MySQL数据库的复制需要启动三个线程来实现: 其中1个在主服务器上,另两个在从服务器上.当发出START SLAVE时,从服务器创建一个I/O ...

  8. 读书笔记 effective c++ Item 52 如果你实现了placement new,你也要实现placement delete

    1. 调用普通版本的operator new抛出异常会发生什么? Placement new和placement delete不是C++动物园中最常遇到的猛兽,所以你不用担心你对它们不熟悉.当你像下面 ...

  9. 修改类不用重启Tomcat加载整个项目

    可以修改类不用重启Tomcat加载整个项目(手工启动) 配置reloadable=true(自动重载) 使用Debug模式,前提是仅限于局部修改.(修改类不用重启--热加载) Tomcat轻小,而We ...

  10. [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?

    情况是这样的,使用NotificationManager触发多个Notification: private Notification genreNotification(Context context ...