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

思路

题意:给定四个长度为n的数组A, B, C, D。 从每个数组中取一个数, 这样得到四个数, 并且这四个数的之和为0. 求这样组合的个数。

题解:直接算出组合数的话,复杂度太高,分成两堆来求,算出 A[i] + B[i] 的值,然后在A[i] + B[i]中找 等于 -C[i] - D[i] 的个数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 4005;
int cd[maxn*maxn];

int main()
{
	int N;
	while (~scanf("%d",&N))
	{
		int a[maxn],b[maxn],c[maxn],d[maxn];
		for (int i = 0;i < N;i++)	scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
		for (int i = 0;i < N;i++)	for (int j = 0;j < N;j++)	cd[i*N+j] = c[i] + d[j];
		sort(cd,cd + N*N);
		int res = 0;
		for (int i = 0;i < N;i++)
		{
			for (int j = 0;j < N;j++)
			{
				int tmp = -a[i] - b[j];
				int pos1 = lower_bound(cd,cd + N*N,tmp) - cd;
				int pos2 = upper_bound(cd,cd + N*N,tmp) - cd;
				res += pos2 - pos1;
			}
		}
		printf("%d\n",res);
	}
	return 0;
}

  

POJ 2785 4 Values whose Sum is 0(想法题)的更多相关文章

  1. POJ 2785 4 Values whose Sum is 0

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

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

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

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

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

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

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

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

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

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

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

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

  9. POJ 2785 4 Values whose Sum is 0 (二分)题解

    思路: 如果用朴素的方法算O(n^4)超时,这里用折半二分.把数组分成两块,分别计算前后两个的和,然后枚举第一个再二分查找第二个中是否有满足和为0的数. 注意和有重复 #include<iost ...

随机推荐

  1. SQL谜题(加减符号替代)

    问题:将以下字符串”.1.2.3.4.5.6.7.8.9 = 1“中的符号点(.)更改为符号加(+)或符号(-),有多少种方法?请用SQL解决此问题 计算过程: CREATE TABLE #(VAL ...

  2. SQLserver 查看数据库包含指定数据的表(字段)

    找出所有字段 1 select a.name as columnname,object_name(a.id)as tablename into t from syscolumns a, sysobje ...

  3. MFC 不让程序显示在任务栏上

    如果是对话框程序直接在对话框的 初始化时,修改样式 ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW); 但是对于多文档或是单文档,则没有对应的对话框样式需 ...

  4. java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET

    java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了!      社区福利快来领取免费参加MDCC大会机会哦    Tag功能介绍—我们 ...

  5. html5实现微信摇一摇功能

    在HTML5中,DeviceOrientation特性所提供的DeviceMotion事件封装了设备的运动传感器时间,通过改时间可以获取设备的运动状态.加速度等数据(另还有deviceOrientat ...

  6. arm-none-eabi-gcc,makefile,stm官方库构建stm32f4xx工程

    参考文章:http://www.stmcu.org/module/forum/forum.php?mod=viewthread&tid=603753&highlight=ubuntu ...

  7. VFL语言使用

  8. Java 集合系列08之 List总结

    一.List概述 1. List是一个接口,它继承于Collection接口,代表有序集合 2. ArrayList, LinkedList, Vector, Stack是List的4个实现类. Ar ...

  9. 支持多文件上传,预览,拖拽,基于bootstrap的上传插件fileinput 的ajax异步上传(转载)

    首先需要导入一些js和css文件 <link href="__PUBLIC__/CSS/bootstrap.css" rel="stylesheet"&g ...

  10. CSRF的防御解决过程

    CSRF是什么,就不多说,网络上的帖子多的去了,关于其定义. 这里主要介绍我们项目中,是如何解决这个问题的.方案比较简单,重点是介绍和记录一下遇到的问题和一些小的心得. 1. 解决方案 A. 用户登录 ...