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

6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45

Sample Output

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).
 #include<cstdio>
#include<string.h>
#include<algorithm>
#define MAXN 4400
using namespace std;
int A[MAXN],B[MAXN],C[MAXN],D[MAXN];
int S[MAXN*MAXN];
int lower_bound1(int low,int high,int num,int a[])
{
int mid;
while(low<high)
{
mid=low+(high-low)/;
if(a[mid]>=num) high=mid;
else low=mid+;
}
return low;
}
int upper_bound1(int low,int high,int num,int a[])
{
int mid;
while(low<high)
{
mid=low+(high-low)/;
if(a[mid]<=num) low=mid+;
else
high=mid;
}
return low;
}
int main()
{
int n,i;
int p;
int cout=;
int l,r,j;
while(scanf("%d",&n)!=EOF)
{
cout=;
for(i=;i<n;i++)
scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]);
p=;
for(i=;i<n;i++)
for(j=;j<n;j++)
S[p++]=A[i]+B[j];
sort(S,S+p);
for(i=;i<n;i++)
for(j=;j<n;j++)
{
int t=C[i]+D[j];
l=lower_bound1(,p,-t,S);
r=upper_bound1(,p,-t,S);
cout+=(r-l);
}
printf("%d\n",cout);
}
return ;
}

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. VS复习 -- if···else和if···else嵌套语句

    注意:理清逻辑,画出逻辑分支图,理清思路 1.if语句 2.if...else语句 3.if..else if...else static void Main(string[] args) { Con ...

  2. vsto publish后无法弹出winform窗口

    http://www.cnblogs.com/xiyang1011/archive/2011/06/07/2074025.html - - 没有调用form.show()...

  3. 为什么 Node.js 这么火,而同样异步模式 Python 框架 Twisted 却十几年一直不温不火?

    twisted是一个强大的异步网络框架,应用的面也非常广,但是没有这几年才出现的Node.js火,社区.文档也是很少可怜我觉得二者其实在本质上差不多,而且python使用起来还是比较容易一些的 匿名用 ...

  4. FZU 2028 时空门问题

    题目链接:时空门问题 简单bfs,每个格子移动的方式除了上下左右,还有时空门,开始想着用邻接表保存每个点能通过时空门到达的点就ok了.很快的敲出来,很快的WA了.长久的dbug并没有发现error.然 ...

  5. phonegap开发入门

    做了几次开发配置了,但时间一长就忘了,特记录一下. 一.环境变量配置::右击“我的电脑”-->"高级"-->"环境变量" 1.在系统变量里新建JAV ...

  6. svn自动更新

    果对svn不熟悉,当svn上面有更新时,想看到实时效果,就得去web目录手动更新,比较麻烦 其它svn有一个自动更新的功能 利用 hook   在svn 仓库目录下面有一个hook目录 在post-c ...

  7. Android Phonebook编写联系人UI加载及联系人保存流程(一)

    2014-01-06 17:05:11 将百度空间里的东西移过来. 本文适合ROM定制做Phonebook的童鞋看,其他人飘过即可- Phonebook添加/编辑联系人UI加载及保存联系人流程,是一系 ...

  8. JSP初识

    JSP最终会变成一个完整的servlet在web应用中运行.它与其他的servlet非常相似,只不过这个servlet类会由容器编写. 1.JSP的生命周期 如果一个web应用包含JSP,部署这个应用 ...

  9. Android再次激活Activity时触发事件用于列表重新读取载入

    @Override protected void onResume(){ super.onResume(); getList(); } 重载 onResume() 方法

  10. SharePoint安全 - SharePoint网站常用页面URL索引

    博客地址 http://blog.csdn.net/foxdave 一. 主要网站内容 首页 /default.aspx /Pages/default.aspx 网站设置 /_layouts/sett ...