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

sign,抱着脑袋想了好久怎么用hash做,就是没想着用二分,,,,,,

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cstdio>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<map>
 9 #include<set>
10 #include<stack>
11 #include<string>
12
13 #define N 4002
14 #define M 10
15 #define mod 1000000007
16 //#define p 10000007
17 #define mod2 1000000000
18 #define ll long long
19 #define LL long long
20 #define eps 1e-9
21 #define inf 0x3fffffff
22 #define maxi(a,b) (a)>(b)? (a) : (b)
23 #define mini(a,b) (a)<(b)? (a) : (b)
24
25 using namespace std;
26
27 int x[N*N];
28 int a[N],b[N],c[N],d[N];
29 ll ans;
30 int k;
31 int n;
32
33 void ini()
34 {
35     int i,j;
36     ans=0;
37     for(i=1;i<=n;i++){
38         scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
39     }
40     k=0;
41     for(i=1;i<=n;i++){
42         for(j=1;j<=n;j++){
43             x[k]=a[i]+b[j];k++;
44         }
45     }
46     sort(x,x+k);
47 }
48
49 void solve()
50 {
51     int i,j;
52     int te;
53     int t1,t2;
54     for(i=1;i<=n;i++){
55         for(j=1;j<=n;j++){
56             te=-c[i]-d[j];
57             t1=lower_bound(x,x+k,te)-x;
58             t2=upper_bound(x,x+k,te)-x;
59             ans+=(ll)(t2-t1);
60         }
61     }
62 }
63
64 void out()
65 {
66     printf("%I64d\n",ans);
67 }
68
69 int main()
70 {
71     //freopen("data.in","r",stdin);
72    // freopen("data.out","w",stdout);
73     //scanf("%d",&T);
74     //for(int ccnt=1;ccnt<=T;ccnt++)
75     //while(T--)
76     while(scanf("%d",&n)!=EOF)
77     {
78         ini();
79         solve();
80         out();
81     }
82
83     return 0;
84 }
时间: 2024-12-12 16:54:31

POJ 2785 4 Values whose Sum is 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

POJ 2785 4 Values whose Sum is 0 (对半分解 二分搜索)

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 17658   Accepted: 5187 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(折半枚举)

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

POJ 2785 4 Values whose Sum is 0

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

题意: 给4个集合ABCD,问有多少种从中各取一个数和为0的方案. 分析: 枚举前两个数建哈希表,枚举后两个数查找即可. 代码: //poj 2785 //sep9 #include <iostream> using namespace std; const int maxN=4012; const int maxM=3999972; int a[maxN],b[maxN],c[maxN],d[maxN]; int hash[maxM+10]; int e; struct Edge { int

poj 2785 4 Values whose Sum is 0 折半枚举

题目链接:http://poj.org/problem?id=2785 枚举的一般思路就是先把所有的状态枚举出来 最后一次性判断该状态合法不合法 而折半枚举的思想是 先枚举一半的状态 把他们的状态存起来 排序 然后再枚举剩下一般 用目标反推前一半的期望状态 接下来在前一半的结果数组中查找是否有相应结果 之所以能优化是因为结果数组有序 就可以用二分搜索 复杂度从O(n^2 * n^2) 降到 O(n^2 * log(n^2))即(O(n^2 * log n)) 二分搜索的一个技巧 在有序数组中用二

poj 2785 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