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. 你需要知道的三个 CSS3技巧(转)

    1. 在CSS中用attr()显示HTML属性值 attr()功能早在CSS 2.1标准中就已经出现,但现在才开始普遍流行.它提供了一个巧妙的方法在CSS中使用HTML标签上的属性,在很多情况下都能帮 ...

  3. BZOJ3996 [TJOI2015]线性代数

    就是求$D = A \times B \times A^T - C \times A^T$ 展开也就是$$D = \sum_{i, j} A_i * A_j * B_{i, j} - \sum_{i} ...

  4. mysql 无法启动1067

    关键字:mysql无法启动办事,mysql卡死,InnoDB"" registration as a STORAGE ENGINE failed.Unknown/unsupport ...

  5. Protocol Buffers in HBase

    For early Hbase developers, it is often a nightmare to understand how the different modules speak am ...

  6. [Jquery]导航菜单效果-纵向

    $( document ).ready( function(e){ var $catCont = $( ".cat-cont" );    //二级菜单div    var $ca ...

  7. bzoj 2330: [SCOI2011]糖果

    #include<cstdio> #include<iostream> using namespace std; ],next[],u[],v[],h,t,a[]; ],f[] ...

  8. FOJ 2105 Digits Count

    题意:对一串数字进行抑或某数,和某数,或某数,统计某区间和的操作. 思路:因为化成二进制就4位可以建4颗线段树,每颗代表一位二进制. and 如果该为是1  直接无视,是0则成段赋值为0: or  如 ...

  9. uboot启动内核(3)

    nand read.jffs2 0x30007FC0 kernel; 从NAND读出内核:从哪读,从kernel分区 放到哪去   -0x30007FC0 nand read.jffs2 0x3000 ...

  10. 编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码

    转自:编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码 JDK:java version “1.8.0_31”Java(TM) SE Runtime Environment ( ...