POJ2785:4 Values whose Sum is 0(二分)

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

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

题意:每列找一个数,得到和为0的序列,有几种不同的方案

对1,2列的数求一个和,3,4列的数求一个和,然后进行二分查找

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define w(a) while(a)
#define ll long long
int n,a[4005],b[4005],c[4005],d[4005],sum1[16000005],sum2[16000005],len;

int main()
{
    int i,j,ans,l,r,mid;
    w(~scanf("%d",&n))
    {
        up(i,0,n-1)
        scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
        len=0;
        up(i,0,n-1)
        up(j,0,n-1)
        sum1[len++]=a[i]+b[j];
        len=0;
        up(i,0,n-1)
        up(j,0,n-1)
        sum2[len++]=c[i]+d[j];
        ans=0;
        sort(sum2,sum2+len);
        up(i,0,len-1)
        {
            l=0,r=len-1;
            w(l<r)
            {
                mid=(l+r)>>1;
                if(sum2[mid]<-sum1[i])
                    l=mid+1;
                else
                    r=mid;
            }
            while(sum2[l]==-sum1[i]&&l<len)
            {
                ans++;
                l++;
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}

POJ2785:4 Values whose Sum is 0(二分)

时间: 2024-10-26 02:59:09

POJ2785:4 Values whose Sum is 0(二分)的相关文章

POJ2785 4 Values whose Sum is 0(二分)

题目链接: http://poj.org/problem?id=2785 题意: 给定你四个大小为n的集合 然后又多少个四元组的和为0,分别来自这四个集合: 分析: 分成两组,得到sum1[[,sum2[],然后排下序,在sum2中二分搜索-sum1[i]记下数就可以了 代码如下: #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using names

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

4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: 5778 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 man

4 Values whose Sum is 0(二分)

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 21370   Accepted: 6428 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

POJ 2785 4 Values whose Sum is 0 [二分]

传送门 13773503 njczy2010 2785 Accepted 25248K 7079MS G++ 1423B 2015-01-11 10:26:48 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 16102   Accepted: 4659 Case Time Limit: 5000MS Description The SUM problem can be

POJ2785 4 Values whose Sum is 0 【枚举】

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 16000   Accepted: 4625 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

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

题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时,把他们看作不同的数字. 做法是把前两个数列和的值存在一个数组(A)中 , 后两个数列的和存在另一个数组(B)中 , 数组都为n^2 . 然后将B数组sort一下 , 将A数组遍历 , 二分查找一下B数组中与A数组元素和为0的个数 . 有个注意的点是万一A数组都是0 , 而B数组都为0的情况(还有其

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

2017-08-01 21:29:14 writer:pprp 参考:http://blog.csdn.net/piaocoder/article/details/45584763 算法分析:直接暴力复杂度过高,所以要用二分的方法,分成两半复杂度就会大大降低: 题目意思:给定4个n(1<=n<=4000)元素的集合 A.B.C.D ,从4个集合中分别选取一个元素a, b,c,d.求满足 a+b+c+d=0的个数 代码如下: //首先将前两列任意两项相加得到数组x,再将后两列任意两项相加取反得到

poj 2785 4 Values whose Sum is 0(sort+二分)

题意: 给你ABCD四个集合,集合中数的个数都为N(N<=4000),如果分别在ABCD四个集合中取一个数,a b c d ,求有多少种可能使得a+b+c+d=0. 当然你可以尝试枚举所有的组合,绝对可以计算出结果,大概有N^4种吧,如果你有足够的时间还是可以算出来的,哈哈. 当然我不是用上面一种方法计算的,那样算肯定超时. 我的做法是求出所有a+b 到ab数组中, 和所有 c+d到cd数组中,然后排序,枚举每个ab,用二分在cd中查找有没有可能组成0.  有个问题就是二分只能返回一个结果,所以

poj 2785 4 Values whose Sum is 0 (简单二分)

//每列选一个数相加为0的个数 # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int ab[4010*4010],cd[4010*4010]; int main() { int n,i,k,j,count,a[4010],b[4010],c[4010],d[4010]; while(~scanf("%d",&n)) { f