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

本文介绍了一种解决四值之和为零问题的算法,通过使用哈希表和二分查找,实现了在给定四个整数列表的情况下,快速计算出所有和为零的四元组数量。该方法适用于列表长度较大(最多4000个元素)的场景,展示了在复杂数据处理中应用高效数据结构和算法的重要性。

传送门

13773503njczy20102785Accepted25248K7079MSG++1423B2015-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 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).

Source

 
 
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 }

 

转载于:https://www.cnblogs.com/njczy2010/p/4216180.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值