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的情况(还有其他情况) , 那二分只能找到一个与之符合的情况 . 所以可以再找刚好比符合数大的数 , 相减他们的位置, 然后加起来 , 就是答案.

这里可以用到两个函数lower_bound()和upper_bound() ,都用了二分查找,前面的函数是返回一个数组中大于或等于一个数的位置,后面的是返回大于这个数的位置(不懂的可以Google or baidu一下这两个函数怎么用).

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4
 5 using namespace std;
 6 const int MAXN = 4010;
 7 typedef long long LL;
 8
 9 LL a[MAXN] , b[MAXN] , c[MAXN] , d[MAXN];
10 LL num1[MAXN * MAXN] , num2[MAXN * MAXN];
11
12 int main()
13 {
14     int n;
15     while(~scanf("%d" , &n)) {
16         for(int i = 0 ; i < n ; i++) {
17             scanf("%lld %lld %lld %lld" , a + i , b + i , c + i , d + i);
18         }
19         int f = 0;
20         for(int i = 0 ; i < n ; i++) {
21             for(int j = 0 ; j < n ; j++) {
22                 num1[f] = a[i] + b[j];
23                 num2[f++] = c[i] + d[j];
24             }
25         }
26         LL res = 0;
27         int temp = 0;
28         sort(num2 , num2 + f);
29         for(int i = 0 ; i < f ; i++) {
30             temp = lower_bound(num2 , num2 + f , -num1[i]) - num2;
31             if(temp < f && num2[temp] + num1[i] == 0) {
32                 res += upper_bound(num2 , num2 + f , -num1[i]) - num2 - temp;
33             }
34         }
35         cout << res << endl;
36     }
37 }
时间: 2024-08-01 22:46:53

POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)的相关文章

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