您的位置:首页 > 产品设计 > UI/UE

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

2018-04-07 15:30 459 查看
题目链接:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: