Time Limit:15000MS     Memory Limit:228000KB     64bit IO Format:%I64d & %I64u

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 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
  1. -36 30 -75 -46
  2. 26 -38 -10 62
  3. -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<string.h>
  3. #include<algorithm>
  4. #define MAXN 4400
  5. using namespace std;
  6. int A[MAXN],B[MAXN],C[MAXN],D[MAXN];
  7. int S[MAXN*MAXN];
  8. int lower_bound1(int low,int high,int num,int a[])
  9. {
  10. int mid;
  11. while(low<high)
  12. {
  13. mid=low+(high-low)/;
  14. if(a[mid]>=num) high=mid;
  15. else low=mid+;
  16. }
  17. return low;
  18. }
  19. int upper_bound1(int low,int high,int num,int a[])
  20. {
  21. int mid;
  22. while(low<high)
  23. {
  24. mid=low+(high-low)/;
  25. if(a[mid]<=num) low=mid+;
  26. else
  27. high=mid;
  28. }
  29. return low;
  30. }
  31. int main()
  32. {
  33. int n,i;
  34. int p;
  35. int cout=;
  36. int l,r,j;
  37. while(scanf("%d",&n)!=EOF)
  38. {
  39. cout=;
  40. for(i=;i<n;i++)
  41. scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]);
  42. p=;
  43. for(i=;i<n;i++)
  44. for(j=;j<n;j++)
  45. S[p++]=A[i]+B[j];
  46. sort(S,S+p);
  47. for(i=;i<n;i++)
  48. for(j=;j<n;j++)
  49. {
  50. int t=C[i]+D[j];
  51. l=lower_bound1(,p,-t,S);
  52. r=upper_bound1(,p,-t,S);
  53. cout+=(r-l);
  54. }
  55. printf("%d\n",cout);
  56. }
  57. return ;
  58. }

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: 20334   A ...

  2. POJ 2785 4 Values whose Sum is 0

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

  3. 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏

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

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

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

  5. K - 4 Values whose Sum is 0(中途相遇法)

    K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limi ...

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

  7. UVA1152-4 Values whose Sum is 0(分块)

    Problem UVA1152-4 Values whose Sum is 0 Accept: 794  Submit: 10087Time Limit: 9000 mSec Problem Desc ...

  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. POJ 2785 4 Values whose Sum is 0(折半枚举+二分)

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

  10. POJ:2785-4 Values whose Sum is 0(双向搜索)

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

随机推荐

  1. [译]Quartz 框架 教程(中文版)2.2.x 之第一课 开始使用Quartz框架

    第一课:开始使用Quartz框架 在你使用调度器之前,需要借助一些具体的例子去理解(谁愿意只是猜啊?).你可以使用SchedulerFactory类来达到程序调度的目的.有一些Quartz框架的用户可 ...

  2. JavaScript实现冒泡排序、快速排序、插入排序

    JavaScript实现冒泡排序.快速排序.插入排序 时间:2014-01-09 18:05:51  来源:  作者:胡晗 冒泡排序的基本思想:所谓冒泡就是泡泡一个一个往上冒,让体积最轻的泡泡浮在最上 ...

  3. Java JDK的安装以及环境变量的配置

    安装并配置完Android SDK之后,本想着可以做个简单的APP应用了,只是依然提示我“请确认Java JDK是否安装”类似的报错,于是又进行了Java JDK的安装以及环境变量的配置. 1.下载地 ...

  4. 表单重置reset

    最近一直在做各种页面的“增删改查”,只是在做新增功能的时候,自己一直在使用 reset来清空form表单,原以为这样子的清空方法是万无一失的,可惜最终还是在进行“修改”操作完了之后再“新增”的时候,就 ...

  5. 使用nexus搭建maven仓库(本地私服)

    我们在使用maven的时候,对于项目所依赖的jar包,maven默认会在中央仓库下载jar包,到本地的磁盘目录(如果没有配置则是用户目录下/.m2/repository文件夹下).如果公司内部搭了一个 ...

  6. Centos 下搭建SVN + Apache 服务器(转载)

    安装软件包 ? 1 # yum install httpd ? 1 # yum install mod_dav_svn ? 1 # yum install subversion 2.  验证安装 ? ...

  7. LTP学习

    下载LTP源码和模型文件: https://github.com/linux-test-project/ltp 官方说明文档 http://ltp.readthedocs.org/zh_CN/late ...

  8. IT公司100题-1-二叉树转换为双链表

    问题描述: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表.要求不能创建任何新的结点,只调整指针的指向. 10   /   \  6      14/  \    /   \4   8 1 ...

  9. 如何在windows上搭建ftp服务器

    FTP(File Transfer Protocol)是TCP/IP网络上两台计算机传送文件的协议,使得主机间可以共享文件.目前有很多软件都能实现这一功能,然而windows自带的IIS就可以帮助你搭 ...

  10. Problem A 栈

    Description   You are given a string consisting of parentheses () and []. A string of this type is s ...