4 Values whose Sum is 0
Time Limit: 15000MS   Memory Limit: 228000K
Total Submissions: 29243   Accepted: 8887
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

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). 
 
题意:ABCD四个数列,每个数列抽一个数字,使得和为0,问一共有几组。当一个数列中有多个相同的数字时,把他们作为不同的数字看待。
思路:直接爆搜肯定会超时,将它们对半分成AB和CD再考虑就可以解决。比如在AB中取出a,b后,CD中要取出-a-b,因此先把CD中所有取数字的情况n*n种给纪录下来,然后排序,用二分查找次数。
 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
typedef long long ll;
const int maxn = ;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
} int n;
int a[maxn],b[maxn],c[maxn],d[maxn];
int cd[maxn*maxn]; void solve()
{
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
cd[i*n+j]=c[i]+d[j];
}
sort(cd,cd+n*n);
ll res=;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
int ab=-(a[i]+b[j]);
res+=upper_bound(cd,cd+n*n,ab)-lower_bound(cd,cd+n*n,ab);
}
cout<<res<<endl;
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]); solve();
}

4 Values whose Sum is 0 POJ - 2785的更多相关文章

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

    题目链接 Description The SUM problem can be formulated as follows: given four lists A, B, C, D of intege ...

  2. Divide and conquer:4 Values whose Sum is 0(POJ 2785)

    找四个数的和为0 题目大意:给定四个集合,要你每个集合选4个数字,组成和为0 这题是3977的简单版,只要和是0就可以了 #include <iostream> #include < ...

  3. 4 Values whose Sum is 0 POJ - 2785(二分应用)

    题意:输入一个数字n,代表有n行a,b,c,d,求a+b+c+d=0有多少组情况. 思路:先求出前两个数字的所有情况,装在一个数组里面,再去求后两个数字的时候二分查找第一个大于等于这个数的位置和第一个 ...

  4. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  5. POJ 2785 4 Values whose Sum is 0

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 13069   Accep ...

  6. POJ - 2785 4 Values whose Sum is 0 二分

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25615   Accep ...

  7. POJ 2785 4 Values whose Sum is 0(折半枚举+二分)

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25675   Accep ...

  8. POJ 2785:4 Values whose Sum is 0 二分

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 18221   Accep ...

  9. 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏

    4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 17875 Accepted: ...

随机推荐

  1. 检查python以及django是否安装配置成功

    首先说明下,我使用pycharm作为开发的IDE,在第一次创建django项目的时候,会自动安装django包的.(网上也有很多单独安装的方法),环境变量配置成功后,就是用下面的方法检测安装成功与否. ...

  2. vue-cli脚手架构建了项目如何去除Eslint验证(语法格式验证)

    Eslint是一个语法检查工具,但是限制很严格,在vue文件里面很多空格都会导致红线,取消的方式如下: 1.创建工程的时候,提示是否启用eslint检测的. Use ESLint to lint yo ...

  3. JS中关于clientWidth offsetWidth scrollWidth 等的区别

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  4. Jackson使用手册

    引用jar:jackson-core,jackson-databind,jackson-annotations http://central.maven.org/maven2/com/fasterxm ...

  5. 如何使用VS将项目生成一个安装包?

    VS2010项目的部署与安装winform程序,我想进行安装.1.在解决方案中 ——点击右键——添加 2.然后选择 安装和部署 ——安装向导 可以更改名称 3.点击 下一步 4.然后选择上那3个 5. ...

  6. 05、Win7上openSSH的安装与配置

    05.Win7上openSSH的安装与配置 1.概述 linux上的ssh命令在网络通信场景下非常方便.现在windows也支持ssh方式和远程主机进行访问.如果只是使用ssh简单的访问功能,就需要很 ...

  7. windows mysql忘记密码解决方案

    因为mysql很久之前装的,今天突然想用的时候发现密码不记得,怎一个尴尬了得,所以没办法,只能修改一个新的密码.       在此过程中遇到了几个问题      1.没法进入数据库:      2.修 ...

  8. Project Euler 第一题效率分析

    Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...

  9. IOS 控制器的数据传递 (顺传 and 逆传)

    ● 控制器之间的数据传递主要有2种情况:顺传和逆传 ➢ 顺传 ●  控制器的跳转方向: A ->C ●  数据的传递方向 : A -> C ● 数据的传递方式 : 在A的prepareFo ...

  10. sparkSQL中udf的使用

    在Spark中使用sql时一些功能需要自定义方法实现,这时候就可以使用UDF功能来实现 多参数支持 UDF不支持参数*的方式输入多个参数,例如String*,不过可以使用array来解决这个问题. 定 ...