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. Theano3.3-练习之逻辑回归

    是官网上theano的逻辑回归的练习(http://deeplearning.net/tutorial/logreg.html#logreg)的讲解. Classifying MNIST digits ...

  2. 一些实用的sublime快捷键以及初始设置

    一些常用快捷键 Ctrl + N-------------------新建 Ctrl + F-------------------查找 Ctrl+Shift +k -----------删除一行 Ct ...

  3. Tensorflow学习笔记3:TensorBoard可视化学习

    TensorBoard简介 Tensorflow发布包中提供了TensorBoard,用于展示Tensorflow任务在计算过程中的Graph.定量指标图以及附加数据.大致的效果如下所示, Tenso ...

  4. 使用socket()函数创建套接字

    在Linux中,一切都是文件,除了文本文件.源文件.二进制文件等,一个硬件设备也可以被映射为一个虚拟的文件,称为设备文件.例如,stdin 称为标准输入文件,它对应的硬件设备一般是键盘,stdout ...

  5. 玩转WIN7的MKLINK

    引言: 换了新电脑,终于再次使用上啦WIN7 ,经过一个周每天重装N次系统,终于弄好一个像样的系统啦.由于使用rt7lite对WIN7SP1官方整合包进行了适当精简,所以最终系统的稳定性还得经过一段时 ...

  6. Map集合的应用及其遍历方式

    ---> HashMap :底层基于哈希表      存储原理也使用哈希表来存放的:            往HashMap添加了元素 ,首先会调用键的hashCode方法 获得一个哈希值,然后 ...

  7. 作业成绩 final 20161124-1201 09:00

    final阶段,20161124-1201 09:00. 申诉截止时间 20161206 12:00,微信联系杨贵福. 凡描述需求或BUG时,应给出以下4项: 你期待看到的现象如何 你实际看到的现象 ...

  8. android file path

    问题 出现的异常为:java.lang.IllegalArgumentException: File /mnt/sdcard/crazyit.bin contains a pathseparator. ...

  9. 简单Matrix 的方法说明记录

    查找资料加上自己理解  ,简单说明Android中Matrix怎么用(新手有错误的地方,希望指正,主要自己记录学习用的) Matrix包含一个3 X 3的矩阵,专门用于图像变换匹配. Matrix提供 ...

  10. 线段树 poj3225

    U:把区间[l,r]覆盖成1I:把[-∞,l)(r,∞]覆盖成0    D:把区间[l,r]覆盖成0C:把[-∞,l)(r,∞]覆盖成0 , 且[l,r]区间0/1互换S:[l,r]区间0/1互换 因 ...