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

Source


题目大意:给4列数,每列有n个数字,从每列中取一个数,求四个数相加为零的情况有多少种。
思路:用两个数组,分别记录左边两列和右边两列的数相加的和,再将其中一个数组从小到大排列,将另一数组遍历一边,用二分的方法判断个数。
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[4005][4], sum1[16000001], sum2[16000001];
int main()
{
	int n, mid;

	while (~scanf("%d", &n))
	{
		int k = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%d %d %d %d", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);
		}
		for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
		{
			sum1[k] = a[i][0] + a[j][1];
			sum2[k++] = a[i][2] + a[j][3];
		}
		sort(sum1, sum1 + k);

		int num = 0;
		for (int i = 0; i < k; i++)
		{
			int left = 0, right = k - 1;
			while (left <= right)
			{
				mid = (left + right) / 2;
				if (sum2[i] + sum1[mid] == 0)
				{
					num++;
					for (int j = mid + 1; j < k; j++)
					{
						if (sum2[i] + sum1[j] == 0)
							num++;
						else
							break;
					}
					for (int j = mid - 1; j >= 0; j--)
					{
						if (sum2[i] + sum1[j] == 0)
							num++;
						else
							break;
					}
					break;
				}
				if (sum2[i] + sum1[mid] > 0)
					right = mid - 1;
				else
					left = mid + 1;
			}
		}
		printf("%d\n", num);
	}
	return 0;
}



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

  1. POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找

    2017-08-01 21:29:14 writer:pprp 参考:http://blog.csdn.net/piaocoder/article/details/45584763 算法分析:直接暴力 ...

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

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

  3. POJ 2785 4 Values whose Sum is 0

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

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

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

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

    题目链接: https://cn.vjudge.net/problem/POJ-2785 The SUM problem can be formulated as follows: given fou ...

  6. POJ 2785 4 Values whose Sum is 0(哈希表)

    [题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...

  7. POJ 2785 4 Values whose Sum is 0 Hash!

    http://poj.org/problem?id=2785 题目大意: 给你四个数组a,b,c,d求满足a+b+c+d=0的个数 其中a,b,c,d可能高达2^28 思路: 嗯,没错,和上次的 HD ...

  8. poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))

    Description The SUM problem can be formulated . In the following, we assume that all lists have the ...

  9. [POJ] 2785 4 Values whose Sum is 0(双向搜索)

    题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...

随机推荐

  1. 每天5分钟 玩转OpenStack 目录列表

    最近在学习 OpenStack 的相关知识,一直苦于 OpenStack 的体系庞大以及复杂程度,学习没有进度,停滞不前.偶然机会在 51CTO 上发现了一个热点的专题关于 OpenStack 的,题 ...

  2. ORACLE数据泵使用详解

    来源于:http://blog.sina.com.cn/s/blog_490a0c990100wh4y.html http://blog.csdn.net/jojo52013145/article/d ...

  3. VC2010 调用 Webservice

    开发环境:VC2010,gsoap_2.8.23 http://blog.csdn.net/zhaiwenjuan/article/details/6590941 使用soapcpp2的时候要加参数- ...

  4. VS2015安装 Secondary Installer Setup Failed求解决方案

    个人同步本文博客地址http://aehyok.com/Blog/Detail/64.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章 ...

  5. C++非类型模板参数

    对于函数模板与类模板,模板参数并不局限于类型,普通值也可以作为模板参数.在基于类型参数的模板中,你定义了一些具体的细节来加以确定代码,直到代码被调用时这些细节才被真正的确定.但是在这里,我们面对的是这 ...

  6. Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化

    C. Graph Reconstruction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/3 ...

  7. 初试zabbix

    一.zabbix简介    zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.       zabbix由zabbix server与可选组件zabbix a ...

  8. 工欲善其事必先利其器---SQL在线可视化模型设计,(还可学习拖拽知识)

    作为技术人员,在开发项目中,不可避免的要跟数据库打交道,一个完整的项目正常情况下是讨论完整体需求,有了大致的框框在脑海中后,是需要设计合理的数据库的,这时会有其他的专业的UML建模工具可以使用, 但是 ...

  9. Java之二分查找算法

    算法说明:取中间位置的值与待查字比较.如果比待查字更大,则去列表的前半部分查找,如果比待查字小,则去列表的后半部分查找,直到找到这个待查字,或者返回没有找到这个待查字.其中给定的列表是从大到小排列的有 ...

  10. C#中添加对象到ArrayList的代码

    把开发过程中比较好的一些代码段做个备份,下面代码是关于C#中添加对象到ArrayList的代码. ArrayList alcollect = new ArrayList();string str = ...