POJ 2785 4 Values whose Sum is 0(暴力枚举的优化策略)

题目链接:

https://cn.vjudge.net/problem/POJ-2785

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

 1 /*
 2 问题 给出n行的4个数,这四列数分别是A,B,C,D的集合,问有多少组ABCD相加和为0
 3 解题思路 刚开始没读懂题就开始写了,没想到题意是另一个意思,还是按练习要求做题吧。
 4 读懂了题,脑子里马上跳出4重循环,又一看n最大为4000,还是放弃吧。
 5 看了一下分析,先将a+b的结果与其出现的次数放在map容器里,再将c+d的结果与其出现的次数放在map容器里,最后查找一下,
 6 如果存在则累计结果。但是超时,原因是常数较大时使用map也可能超时。
 7 随后在网上看到一种更为巧妙的解法,将C和D的所有结果存放在一个一维数组中,再将其排序,遍历A+B的和,累加在这个二维数组
 8 中的个数即可。
 9 */
10
11 /*解法一 超时!!!
12 #include<cstdio>
13 #include<iostream>
14 #include<map>
15 using namespace std;
16
17 int main(){
18     int T,n,cou,i,j,a[4010],b[4010],c[4010],d[4010];
19     map<int,int> m1,m2;
20
21     while(scanf("%d",&n) != EOF)
22     {
23         j=0;
24         for(i=1;i<=n;i++){
25             scanf("%d%d%d%d",&a[j],&b[j],&c[j],&d[j]);
26             j++;//不能缩放在上面的一句
27         }
28
29         for(i=0;i<n;i++){
30             for(j=0;j<n;j++){
31                 m1[ a[i]+b[j] ]++;
32             }
33         }
34         for(i=0;i<n;i++){
35             for(j=0;j<n;j++){
36                 m2[ -1*(c[i]+d[j] ) ]++;
37             }
38         }
39
40         map<int,int>::iterator it1,it2;
41         int ans=0;
42         for(it1=m1.begin(); it1 != m1.end(); it1++){
43             it2=m2.find(it1->first);
44             if(it2 != m2.end()){
45                 ans += (it1->second * it2->second);
46             }
47         }
48         printf("%d\n",ans);
49     }
50     return 0;
51 }*/
52 //解法二
53 #include<cstdio>
54 #include<algorithm>
55 using namespace std;
56
57 int cd[4010*4010];//一维数组当二维数组用
58
59 int main(){
60     int T,n,cou,i,j,a[4010],b[4010],c[4010],d[4010],sumab;
61     long long ans;
62     while(scanf("%d",&n) != EOF)
63     {
64         for(i=0;i<n;i++)
65             scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
66
67         for(i=0;i<n;i++){
68             for(j=0;j<n;j++){
69                 cd[i*n+j]=c[i]+d[j];
70             }
71         }
72
73         sort(cd,cd+n*n);
74
75         ans=0;
76         for(i=0;i<n;i++){
77             for(j=0;j<n;j++){
78                 sumab=-1*(a[i]+b[j]);
79                 ans += upper_bound(cd,cd+n*n,sumab) - lower_bound(cd,cd+n*n,sumab);
80                 //使用参数,起点+终点+目标值
81             }
82         }
83
84         printf("%lld\n",ans);
85     }
86     return 0;
87 }
88  

原文地址:https://www.cnblogs.com/wenzhixin/p/8733441.html

时间: 2024-10-12 12:09:19

POJ 2785 4 Values whose Sum is 0(暴力枚举的优化策略)的相关文章

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(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 [二分]

传送门 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

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(折半枚举(双向搜索))

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