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. python中from module import * 的一个陷阱

    from module import *把module中的成员全部导到了当前的global namespace,访问起来就比较方便了.当然,python style一般不建议这么做,因为可能引起nam ...

  2. 理解JavaScript中的参数传递 - leetcode189. Rotate Array

    1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...

  3. DTCMS插件的制作实例电子资源管理(二)Admin后台页面编写

    总目录 插件目录结构(一) Admin后台页面编写(二) 前台模板页编写(三) URL重写(四) 本实例旨在以一个实际的项目中的例子来介绍如何在dtcms中制作插件,本系列文章非入门教程,部分逻辑实现 ...

  4. WPF 异步加载高清大图

    不管什么东西,但凡太大了,总是让人又爱又恨啊!(很有道理的样子,大家鼓掌└( ̄  ̄└)(┘ ̄  ̄)┘) 猿:老板,现在这社会啊,真是浮躁啊,之前还是什么1080P,然后就到了2K,现在又到了4K……他 ...

  5. Nodejs进阶:基于express+multer的文件上传

    关于作者 程序猿小卡,前腾讯IMWEB团队成员,阿里云栖社区专家博主.欢迎加入 Express前端交流群(197339705). 正在填坑:<Nodejs学习笔记> / <Expre ...

  6. git的理解

    1.对git的分支怎么理解. git的本地,git的跟踪,如果我们的本地的命令行进入某个分支的话,我们的本地对应的文件夹就显示某个,然后我们的ide打开的就是那个分支 2.git的在线视频教学 htt ...

  7. Code.R团队展示

    团队成员: 031302620马凛凛(队长) 031302619吕昆明 031302319汪毓顺 031302404陈俊达 团队名称: Code.R 团队项目: 基于web的教师报课系统 团队成员风采 ...

  8. 跨浏览器图像灰度(grayscale)解决方案

    <style type="text/css"> .gray { -webkit-filter: grayscale(100%); /* CSS3 filter方式,we ...

  9. inheritableStatics 与statics类

    /** * statics 可以包含类的静态和静态方法,但是不能被子类继承 * inheritableStatics 与statics类似但是可以被子类继承 */ Ext.onReady(functi ...

  10. 89c51中断入口地址表

    中断源   中断入口地址 外部中断0 0003H 定时器/计数器T0 000BH 外部中断1 0013H 定时器/计数器T1 001BH 串行口中断   0023H