题目链接

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.求出这样的组合的个数,当一个数列中有多个相同的数字时,把它们作为不同的数字看待。

分析:

从四个数列中选择的话总共有n4种情况,所以全部判断一遍不可行。不过将它们对半分成AB和CD再考虑,就可以快速解决了。从两个序列中选择的话只有n2种组合,所以可以进行枚举。先从A、B中选择a,b后,为了使总和为0,则需要从C,D中取出c+d=-a-b。因此先将从A,B中取数字的n^2种方法全部枚举出来,将这些和排好序,这样就可以运用二分搜索了。

有时候,问题的规模较大,无法枚举所有元素的组合,但能够枚举一半元素的组合。此时,将问题拆成两半后分别枚举,再合并它们的结果,这一方法往往非常有效。

代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,a[4009],b[4009],c[4009],d[4009];
int num[16000000];
int main()
{
scanf("%d",&n);
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++)
num[i*n+j]=a[i]+b[j];
sort(num,num+n*n);
long long int ans=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
int cd=-(c[i]+d[j]);
ans+=upper_bound(num,num+n*n,cd)-lower_bound(num,num+n*n,cd); }
printf("%lld\n",ans); return 0;
}

4 Values whose Sum is 0 POJ 2785 (折半枚举)的更多相关文章

  1. 4 Values whose Sum is 0 POJ - 2785

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

  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(折半枚举+二分搜索)

    传送门:Problem 2785 题意: 给定 n 行数,每行都有 4 个数A,B,C,D. 要从每列中各抽取出一个数,问使四个数的和为0的所有方案数. 相同数字不同位置当作不同数字对待. 题解: 如 ...

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

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

  6. POJ 2785 4 Values whose Sum is 0

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

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

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

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

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

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

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

随机推荐

  1. FromHandle临时对象一探究竟

    我们在调用CWnd::GetDlgItem()函数时,MSDN告诉我们:The returned pointer may be temporary and should not be stored f ...

  2. css那些事儿4 背景图像

    background:背景颜色,图像,平铺方式,大小,位置 能够显示背景区域即为盒子模型的填充和内容部分,其中背景图像将会覆盖背景颜色.常见的水平或垂直渐变颜色背景通常使用水平或垂直渐变的背景图像在水 ...

  3. eclipse 创建并运行maven web项目

    这两天想在eclipse上运行maven web项目,折腾了许久,总算success啦. 1,利用eclipse创建dynamic web project(eclipse需要安装m2eclipse). ...

  4. python循环解码base64

    第一次写博客,都不知道该如何下手,写的不是很好,还望各位大佬不要喷我. 先来介绍一下base64: Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打 ...

  5. 9.1、AutoEncoder自动编码器[转]

    如果给定一个神经网络,我们假设其输出与输入是相同的,然后训练调整其参数,得到每一层中的权重.自然地,我们就得到了输入I的几种不同表示(每一层代表一种表示),这些表示就是特征.自动编码器就是一种尽可能复 ...

  6. Oracle 转义字符

    id sfds_V_SF ASD_V_DSAF SD_V_DSAD   下划线是Oracle特殊字符,需要转移,如下    select * from systab t where t.id like ...

  7. ARC072E Alice in linear land

    ---题面--- 题解: 首先我们要观察到一个性质,因为在固定的起始距离下,经过固定的操作,最后所在的位置是固定的,我们设经过操作1 ~ i之后所在的地方距离终点为d[i]. 那么如果女巫可以修改第i ...

  8. BZOJ4199:[NOI2015]品酒大会——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4199 https://www.luogu.org/problemnew/show/P2178#su ...

  9. UOJ228:基础数据结构练习题——题解

    http://uoj.ac/problem/228 参考:https://www.cnblogs.com/ljh2000-jump/p/6357583.html 考虑当整个区间的最大值开方==最小值开 ...

  10. AOJ.720 丢失的学妹

    缺失的学妹 考察点 STL MAP Time Mem Len Lang 3.81s 39.1MB 0.68K G++ 题意分析 给出妹子学号的个数n,给出n个学号,和n-1个学号,求在n学号中那个没有 ...