POJ 2785 4 Values whose Sum is 0(折半枚举)

原创 2015年07月06日 20:27:46

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



思路:先把前两列枚举两两组合产生N*N个数,然后在枚举后两列,然后二分求可以等于0的个数就好 复杂度是O(n*nlogn)



#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <cstdio>
#include <ctime>
#include <bitset>
#include <algorithm>
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define refeach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,n) for ( int i=1; i<=int(n); i++ )
using namespace std;
typedef long long ll;

const int N = 4000+100;
int a[5][N];
int all[N*N];
int half[N*N];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        REP(i,n) REP(j,4) scanf("%d",&a[j][i]);
        int top = 0;
        ll ans = 0;
        REP(i,n) REP(j,n) half[++top] = a[1][i]+a[2][j];
        REP(i,n) REP(j,n) all[top--] = a[3][i]+a[4][j];
        sort(all+1,all+n*n+1);
        REP(i,n*n) ans += upper_bound(all+1,all+n*n+1,-half[i])-lower_bound(all+1,all+n*n+1,-half[i]);
        printf("%I64d\n",ans);
    }
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ2785:4 Values whose Sum is 0(二分)

Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer ...
  • libin56842
  • libin56842
  • 2014年08月18日 16:37
  • 2181

4 Values whose Sum is 0+二分答案+POJ

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submission...
  • u012870383
  • u012870383
  • 2014年08月01日 11:32
  • 420

POJ 2785 4 Values whose Sum is 0【双向搜索/折半枚举】

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 21935  ...
  • Puyar_
  • Puyar_
  • 2017年07月03日 16:46
  • 80

4 Values whose Sum is 0 POJ - 2785 (折半枚举)

The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute...
  • elbadaernu
  • elbadaernu
  • 2017年08月22日 17:37
  • 116

【折半枚举 && 二分】POJ - 2785 4 Values whose Sum is 0

Problem Description 给定各有n个整数的四个数列A, B, C, D。要从每个数列中取出1个数,使四个数的和为0。求出这样的组合的个数。当一个数列中有多个相同的数字时,把他们作为...
  • bbbbswbq
  • bbbbswbq
  • 2017年12月21日 11:17
  • 29

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

Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 17715 ...
  • u012848631
  • u012848631
  • 2015年08月03日 15:19
  • 297

POJ 2785 4 VALUES WHOSE SUM IS 0 【折半枚举】

DescriptionThe SUM problem can be formulated as follows: given four lists A, B, C, D of integer valu...
  • u014427196
  • u014427196
  • 2015年06月05日 23:41
  • 363

poj 2785 4 Values whose Sum is 0

4 Values whose Sum is 0Time Limit: 15000MSMemory Limit: 228000KTotal Submissions: 18683Accepted: 554...
  • smilesundream
  • smilesundream
  • 2017年05月20日 10:06
  • 53

poj 2785 4 values whose sum is 0

题目大意:有四列整数,从每列中取出一个数相加,和如果是0,即满足要求,求这样的数的组合有多少种。 方法:由于每列最多可以有4000个数,所以暴力的话会超时,可以先将两列的数的所有组合的和存在两个数组...
  • meizhouyihei
  • meizhouyihei
  • 2015年03月09日 22:34
  • 249

POJ 2785(4 Values whose Sum is 0)

题意:给A,B,C,D四个集合,问有多少个(a, b, c, d ) ∈ A x B x C x D且a+b+c+d=0;  思路:(排序+二分)这题2B了好多次。。。大雾。。。先将A,B和C,D分...
  • joy_go
  • joy_go
  • 2012年09月25日 12:12
  • 401
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 2785 4 Values whose Sum is 0(折半枚举)
举报原因:
原因补充:

(最多只允许输入30个字)