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

Source

Southwestern Europe 2005

/*
** Problem: POJ2785
** Status: Accepted
** Running Time: 6766ms
** Author: Changmu
**
** 题解:将解空间分成两部分分别枚举。
*/

#include <cstdio>
#include <cstring>
#include <algorithm>

#define maxn 4010
typedef __int64 LL;
using namespace std;

int A[4][maxn], CD[maxn * maxn];

int main() {
    LL ret = 0;
    int i, j, n, id = 0, tmp;
    scanf("%d", &n);
    for(i = 0; i < n; ++i) {
        for(j = 0; j < 4; ++j)
            scanf("%d", &A[j][i]);
    }
    for(i = 0; i < n; ++i)
        for(j = 0; j < n; ++j)
            CD[id++] = A[2][i] + A[3][j];
    sort(CD, CD + id);

    for(i = 0; i < n; ++i)
        for(j = 0; j < n; ++j) {
            tmp = A[0][i] + A[1][j];
            ret += upper_bound(CD, CD + id, -tmp) - lower_bound(CD, CD + id, -tmp);
        }
    printf("%I64d\n", ret);
}
时间: 2024-10-29 19:06:31

POJ2785 4 Values whose Sum is 0 【枚举】的相关文章

[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

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的情况(还有其

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

POJ 2785 4 Values whose Sum is 0(折半枚举)

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

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 s

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(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的情况数目. [解题思路] 我们可以考虑前两列的数字相加之和一定与后两列相加和互为相反数,那么我们可以枚举出前两列数字之和,并且,枚举出后两列数据之和的相反数,并对之排序,然后利用二分法进行查找即可. [AC代码] #include<iostream> #include<algorithm> using namespace std; int n,ans,a[4040],b[4040],c[4

1152 - 4 Values whose Sum is 0(好用的hash标记,多重循环简单化)

不得不说这个题就是炫酷啊! 首先说一说思路吧,是这么想的: 1.弄四重循环,爆破,明显会超时. 2.为了处理多重循环,就枚举a+b+c,只需要在d中找到a+b+c的相反数即可,超时 3.枚举a+b,只需要在c+d中找到a+b的相反数即可,TMD超时! 4.循环只能优化到这个程度了,再优化就得用哈希表直接调用了. 这个题的哈希表也是新的知识,由于本题a+b的值可能很大很大,所以以前的哈希表检索不好用了(因为数组开不了那么大),多亏高神的Blog,学了一招,哈哈,下面是搬运的内容: 对于一个无法用数